我在 c++ 应用程序中创建了一个带有标题的 cURL 帖子:
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &bufferdata);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writerResponse);
struct curl_slist *headers=NULL;
char outputmessage[]="";
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers );
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cJSONstring.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, cJSONstring.length());
curl_easy_setopt(curl, CURLOPT_VERBOSE,1);
curl_easy_setopt(curl, CURLOPT_HEADER,1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
curl_multi_add_handle(m_telemetryCurlm, curl);
我创建了一个线程来调用多执行:
void ContentManager::processTelemetry()
{
m_telemetryThreadRunning = true;
while(m_telemetryThreadRunning)
{
static_cast<ADCAnalytics*>(m_adcAnalytics)->performTelemerty();
usleep(5000);
}
}
void ADCAnalytics::performTelemerty()
{
// number of active downloads
int counter = 0;
// preform downloading
curl_multi_perform(m_telemetryCurlm, &counter);
// handle CURL actions (connection errors, download finish, etc.)
checkCURLMessages();
}
但是当我检查 curl 消息时,我得到:
12-10 13:23:00.845: I/ADC(17248): 缓冲区 = HTTP/1.1 400 错误请求
12-10 13:23:00.845: I/ADC(17248): X-TraceUrl: /appstats/details?time=1355145806563&type=json
12-10 13:23:00.845:I/ADC(17248):内容类型:文本/纯文本
12-10 13:23:00.845:I/ADC(17248):变化:接受编码
12-10 13:23:00.845:I/ADC(17248):日期:2012 年 12 月 10 日星期一 13:23:26 GMT
12-10 13:23:00.845:I/ADC(17248):服务器:谷歌前端
12-10 13:23:00.845:I/ADC(17248):缓存控制:私有
12-10 13:23:00.845:I/ADC(17248):传输编码:分块
似乎 curl_multi_add_handle 和 curl_multi_perform 之间的内容类型发生了变化。有人知道这里有什么问题吗?我该怎么做才能避免内容类型发生变化?
添加以下代码时
do {
while(curl_multi_perform(m_telemetryCurlm, &still_running) ==CURLM_CALL_MULTI_PERFORM);
} while (still_running);
设置 cURL 对象后立即。请求被发送到服务器,我得到
12-10 13:11:48.105: I/ADC(16845): 缓冲区 = HTTP/1.1 200 OK
12-10 13:11:48.105: I/ADC(16845): X-TraceUrl: /appstats/details?time=1355145133776&type=json
12-10 13:11:48.105:I/ADC(16845):内容类型:应用程序/json
12-10 13:11:48.105:I/ADC(16845):变化:接受编码
12-10 13:11:48.105:I/ADC(16845):日期:2012 年 12 月 10 日星期一 13:12:13 GMT
12-10 13:11:48.105:I/ADC(16845):服务器:谷歌前端
12-10 13:11:48.105:I/ADC(16845):缓存控制:私有
12-10 13:11:48.105: I/ADC(16845): Transfer-Encoding: chunked
所以,我不相信服务器端有什么问题。
谢谢