当我使用libcurl请求后台,数据过长的时候,请求头部会有一个这样的信息
Expect: 100-continue
请求到后台后,直接报错500了该问体是因为,请求内容过长的时候如果后默认会有一个EXpect的头信息,需要后台进行响应,如果后台没有处理这个头信息则会报错500
这个时候客户端请求的时候可以去掉这个头部信息就可以请求正常了
具体操作如下,加这一行就行了:
headers = curl_slist_append(headers, "Expect:");
具体代码
bool init(CURL*& conn, const char* url, std::string* p_buffer)
{
CURLcode code;
curl_slist *headers=NULL;
conn = curl_easy_init();
if (NULL == conn)
{
qDebug()<< " Failed to create CURL connection" ;
exit(EXIT_FAILURE);
}
headers = curl_slist_append(headers, "Content-type:application/json");
//目前必须添加一个这个空的expect 否则头部信息会默认添加Expect: 100-continue 上传过长交易记录就会失败
headers = curl_slist_append(headers, "Expect:");
code =curl_easy_setopt(conn, CURLOPT_HTTPHEADER, headers);
if (code != CURLE_OK)
{
qDebug()<< " Failed to set error buffer " << code;
return false;
}
code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, error_buffer);
if (code != CURLE_OK)
{
qDebug()<<" Failed to set error buffer " << code;
return false;
}
curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(conn, CURLOPT_VERBOSE, 0L);
code = curl_easy_setopt(conn, CURLOPT_URL, url);
if (code != CURLE_OK)
{
qDebug()<< " Failed to set URL " << error_buffer;
return false;
}
code = curl_easy_setopt(conn, CURLOPT_FOLLOWLOCATION, 1);
if (code != CURLE_OK)
{
qDebug()<<" Failed to set redirect option " << error_buffer;
return false;
}
code = curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, writer);
if (code != CURLE_OK)
{
qDebug()<< " Failed to set writer " << error_buffer;
return false;
}
code = curl_easy_setopt(conn, CURLOPT_WRITEDATA, p_buffer);
if (code != CURLE_OK)
{
qDebug()<< " Failed to set write data " << error_buffer;
return false;
}
return true;
}
static int writer(char* data, size_t size, size_t nmemb, std::string* writer_data)
{
unsigned long sizes = size * nmemb;
if (NULL == writer_data)
{
return 0;
}
writer_data->append(data, sizes);
return sizes;
}