curl使用POS方式发送HTTP数据到服务器慢的问题

    最近几天使用curl开发一个http客户端向HTTP服务器发送数据,在局域网内部发送数据时,需要1.4s的时间,但服务器开发人员使用postman发送数据仅需要300ms则可以完成,在客户端,我们需要发送900KB的数据,在CURL使用默认发送时,会向HTTP发送一个请求报文,询问服务器是否可以接收,但在服务器未配置此回应时,则CURL需要等待1秒的时间,从而导致时间的差异。

    我们在使用curl时可以使用http头关闭此功能,使用http直接发送的方式解决此延时,以下是全部代码,请注意红色一行:

{

CURL *curl = NULL;
    CURLcode res;
    string l_strreponse("");
    /* get a curl handle */
    curl = curl_easy_init();
    if(!curl)
    {
        l_iRet = RES_ERR_CURLINIT;
        g_log->iWriteLog("iPostHttp curl_easy_init fail");
        return l_iRet;
    }
    //设置通讯超时
    res = curl_easy_setopt(curl,CURLOPT_TIMEOUT,25);
    //设置输出细节内容
    res = curl_easy_setopt(curl,CURLOPT_VERBOSE,1L);
    //增加自定义头
    curl_slist* http_header = NULL;
    http_header = curl_slist_append(http_header,"Accept: application/json");
    http_header = curl_slist_append(http_header,"Content-Type: application/json");
    http_header = curl_slist_append(http_header,"charsets: utf-8");
    http_header = curl_slist_append(http_header,"Expect:");     //增加此头部可以减少http交互时间,减少1秒
    //附加headers
    std::list::iterator lstpos;
    for(lstpos = lstheaders.begin();lstpos != lstheaders.end();lstpos++)
    {
        http_header = curl_slist_append(http_header,(*lstpos).c_str());
    }

    res = curl_easy_setopt(curl,CURLOPT_HTTPHEADER,http_header);
    //res = curl_easy_setopt(curl,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
    //设置为post模式
    res = curl_easy_setopt(curl,CURLOPT_CUSTOMREQUEST,"POST");
    res = curl_easy_setopt(curl, CURLOPT_URL,str_url.c_str());
    if(res != CURLE_OK)
    {
        l_iRet = res;
        g_log->iWriteLog("iPostHttp CURLOPT_URL error:%s url:%s",curl_easy_strerror(res),str_url.c_str());
        goto iPostHttpend;
    }
    res = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, str_senddata.length());
    res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS,str_senddata.c_str());
    if(res != CURLE_OK)
    {
        l_iRet = res;
        g_log->iWriteLog("iPostHttp CURLOPT_POSTFIELDS error:%s data:%s",curl_easy_strerror(res),str_senddata.c_str());
        goto iPostHttpend;
    }
    res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,curl_write_data);
    res = curl_easy_setopt(curl, CURLOPT_WRITEDATA,&l_strreponse);

    res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER,false);
    res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST,false);
    res = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1);
    //设置显示进度
    //res = curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, curl_progress);
    //res = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false);

     /* Perform the request, res will get the return code */
    g_log->iWriteLog("iPostHttp curl_easy_perform begin");
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
    {
        l_iRet = res;
        g_log->iWriteLog("iPostHttp curl_easy_perform error:%s",curl_easy_strerror(res));
    }
    else
    {
        l_iRet = res;

        g_log->iWriteLog("iPostHttp curl_easy_perform ok msg:%s",str_result.c_str());
    }
iPostHttpend:
    if(curl)
        ::curl_easy_cleanup(curl);

}



PS:使用codeblocks可以编译curl的库,编译使用的指令如下

"C:\Program Files (x86)\CodeBlocks\MinGW\bin\mingw32-make.exe" .\lib\Makefile.m32

你可能感兴趣的:(curl)