我正在尝试使用libcurl c api将某些内容发布到网站上。当我从浏览器访问它时,URL工作。我从浏览器栏中复制了相同内容并在程序中访问它。该程序挂起curl_easy_perform调用,永不返回。我还提供了write_callback但它永远不会被调用。 下面的这段代码有什么问题
出于隐私原因,我从下面的代码中删除了网址。
int main(void)
{
CURLcode res;
CURL *curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "");
curl_easy_setopt(curl, CURLOPT_POST, 1);
//curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1L );
//curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1L );
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
res = curl_easy_perform(curl);
if (res != CURLE_OK) fprintf(stderr,"failed: %s", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
return -1;
}
答案 0 :(得分:0)
您可能还需要设置CURLOPT_POSTFIELDS
和CURLOPT_POSTFIELDSIZE
。
因此,如果您的帖子看起来像http://awesome.com/superpost.html?blah=hi
curl网站上有一些不错的例子:
注意:您还希望使用curl_escape
答案 1 :(得分:0)
问题可能出在URL编码类型上。因此,请尝试查看如何正确编码发送到该网址的数据。根据服务器。在php中,您可以使用rawurlencode
与url-encode
不同,并且可能会使服务器回复。