libcurl-发布后发布
我正在使用libcurl用C编写一个http客户机。但是,当使用相同的句柄传输libcurl-发布后发布,c,http,post,libcurl,C,Http,Post,Libcurl,我正在使用libcurl用C编写一个http客户机。但是,当使用相同的句柄传输PUT后接POST时,我面临一个奇怪的问题。下面是一个示例代码: #include <curl/curl.h> void send_a_put(CURL *handle){ curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L); //PUT curl_easy_setopt(handle, CURLOPT_INFILESIZE, 0L); cur
PUT
后接POST
时,我面临一个奇怪的问题。下面是一个示例代码:
#include <curl/curl.h>
void send_a_put(CURL *handle){
curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L); //PUT
curl_easy_setopt(handle, CURLOPT_INFILESIZE, 0L);
curl_easy_perform(handle);
}
void send_a_post(CURL *handle){
curl_easy_setopt(handle, CURLOPT_POST, 1L); //POST
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, 0L);
curl_easy_perform(handle);
}
int main(void){
CURL *handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, "http://localhost:8888/");
curl_easy_setopt(handle, CURLOPT_HTTPHEADER,
curl_slist_append(NULL, "Expect:"));
curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L); //for debug
send_a_put(handle);
send_a_post(handle);
curl_easy_cleanup(handle);
return 0;
}
更改顺序可使两次传输正确进行(即send_a_post()
然后send_a_put()
)。如果我在PUT
之后或POST
之前发送GET
,一切都会顺利进行。只有在PUT
后接POST
时才会出现问题
有人知道为什么会发生这种情况吗?“如果发出POST请求,然后希望使用相同的重复使用句柄生成HEAD或GET,则必须使用CURLOPT_NOBODY或CURLOPT_HTTPGET或类似的方法显式设置新的请求类型。”
从
编辑:实际上比这更简单。您需要在两次通话之间重置选项,如下所示:
void
send_a_put (CURL * handle)
{
curl_easy_setopt (handle, CURLOPT_POST, 0L); // disable POST
curl_easy_setopt (handle, CURLOPT_UPLOAD, 1L); // enable PUT
curl_easy_setopt (handle, CURLOPT_INFILESIZE, 0L);
curl_easy_perform (handle);
}
void
send_a_post (CURL * handle)
{
curl_easy_setopt (handle, CURLOPT_UPLOAD, 0L); // disable PUT
curl_easy_setopt (handle, CURLOPT_POST, 1L); // enable POST
curl_easy_setopt (handle, CURLOPT_POSTFIELDSIZE, 0L);
curl_easy_perform (handle);
}
如果发出POST请求,然后希望使用相同的重复使用句柄生成HEAD或GET,则必须使用CURLOPT_NOBODY或CURLOPT_HTTPGET或类似方法显式设置新的请求类型
从
编辑:实际上比这更简单。您需要在两次通话之间重置选项,如下所示:
void
send_a_put (CURL * handle)
{
curl_easy_setopt (handle, CURLOPT_POST, 0L); // disable POST
curl_easy_setopt (handle, CURLOPT_UPLOAD, 1L); // enable PUT
curl_easy_setopt (handle, CURLOPT_INFILESIZE, 0L);
curl_easy_perform (handle);
}
void
send_a_post (CURL * handle)
{
curl_easy_setopt (handle, CURLOPT_UPLOAD, 0L); // disable PUT
curl_easy_setopt (handle, CURLOPT_POST, 1L); // enable POST
curl_easy_setopt (handle, CURLOPT_POSTFIELDSIZE, 0L);
curl_easy_perform (handle);
}
是的,我知道。问题不在于
GET
或HEAD
方法,而是当发送PUT
后接POST
时。这与我描述的行为相同。。您只需要设置正确的选项。对于libcurl,设置CURLOPT\u NOBODY
意味着HEAD
,CURLOPT\u HTTPGET
意味着GET
。好啊在我展示的代码中,我已经在send\u a\u POST()
函数中将新的请求类型设置为CURLOPT\u POST
。请参见上文。或者你可以使用多个手柄。是的,我知道。问题不在于GET
或HEAD
方法,而是当发送PUT
后接POST
时。这与我描述的行为相同。。您只需要设置正确的选项。对于libcurl,设置CURLOPT\u NOBODY
意味着HEAD
,CURLOPT\u HTTPGET
意味着GET
。好啊在我展示的代码中,我已经在send\u a\u POST()
函数中将新的请求类型设置为CURLOPT\u POST
。请参见上文。或者,您可以只使用多个句柄。