libcurl HTTP POST不起作用

时间:2017-05-23 16:58:55

标签: c++ http libcurl

我正在尝试使用libcurl发送HTTPS POST请求。 这是代码:

CURL* curl = curl_easy_init();
if (!curl) return "";

CURLcode res;
std::string request = "https://some-site.com/test.php";
std::string response;

curl_easy_setopt(curl, CURLOPT_URL, request);
curl_easy_setopt(curl, CURLOPT_POST, true);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "email=test&pass=test");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies.txt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);

res = curl_easy_perform(curl);

curl_easy_cleanup(curl);

这是WriteCallback:

size_t WriteCallback(void* ptr, size_t size, size_t count, void* stream)
{
    ((std::string*)stream)->append((char*)ptr, 0, size * count);
    return size * count;
}

当我发送请求时,字段已成功填写,但表单未发送。我没有重定向到另一个页面。 提前谢谢。

0 个答案:

没有答案