CURLOPT_TIMEOUT 超时问题 导致 curl_easy_perform返回28

文章描述了通过CURL发送图片时遇到的超时问题,通过调试和调整超时设置解决了问题。

    今天在测试排查一个奇怪的问题,图片发送有时会发不出去,(50%的概率)一开始觉得是网络

的问题或者是对方服务器有问题,ping了一下,反应还是挺快的,而且其它设备对外网的反应还是

好的,进入设备查看打印,没发送出去的curl_easy_perform返回值28

    _pcResponse[0] = '\0';
    (void)curl_easy_setopt(pstCurlEasy, CURLOPT_URL, _pcUrl);
    (void)curl_easy_setopt(pstCurlEasy, CURLOPT_READFUNCTION, NULL); 
    (void)curl_easy_setopt(pstCurlEasy, CURLOPT_POST, 1);
    (void)curl_easy_setopt(pstCurlEasy, CURLOPT_WRITEFUNCTION, OnCurlRecvData);  
    (void)curl_easy_setopt(pstCurlEasy, CURLOPT_WRITEDATA, (void *)_pcResponse);
    (void)curl_easy_setopt(pstCurlEasy, CURLOPT_POSTFIELDS, _pcPostBuf);
    (void)curl_easy_setopt(pstCurlEasy, CURLOPT_POSTFIELDSIZE, (long)strlen(_pcPostBuf)); 
    (void)curl_easy_setopt(pstCurlEasy, CURLOPT_TIMEOUT, 3L);
    (void)curl_easy_setopt(pstCurlEasy, CURLOPT_HEADER, 1);
    http_headers = curl_slist_append(http_headers, "Accept: application/json,application/VIID+json");
    http_headers = curl_slist_append(http_headers, "Content-Type: application/VIID+JSON;charset=UTF-8");
    http_headers = curl_slist_append(http_headers, "Connection: keepalive");
    http_headers = curl_slist_append(http_headers, cUserIdentify);
    http_headers = curl_slist_append(http_headers, _AuthBuf);
    http_headers = curl_slist_append(http_headers, "User-Agent: libghttp/1.0");
    
    (void)curl_easy_setopt(pstCurlEasy, CURLOPT_HTTPHEADER, http_headers);
    iRet = curl_easy_perform(pstCurlEasy);
    if(iRet != CURLE_OK)
    {
        Gat1400Log_Error("iRet = %d\n", iRet);
        goto end;
    }

于是进入搜索了一下为什么返回28,查了一下是因为超时,于是根据网络上的添加相应设置

 curl_easy_setopt(pEasy, CURLOPT_VERBOSE, 1);

再进行运行,出现了下面的

一看就知道了,和服务器交互的时间很显示大于了设置的时间3L,然后我尝试把超时时间加大

(void)curl_easy_setopt(pstCurlEasy, CURLOPT_TIMEOUT, 30L);

然后再运行,放入测试的环境(在我自己的电脑上3秒就够了,不会出现测试的那种超时现象。。。。)

果然就好使了。

int cloud_https_post(const char *pUrl, const char *request, char **response, st_http_resinfo *pHttpResInfo) { CURLcode res; CURL* curl = NULL; struct curl_slist *headers = NULL; #ifdef CLOUD_HTTPS_DEBUG char errbuf[CURL_ERROR_SIZE]; memset(errbuf, '\0', CURL_ERROR_SIZE); #endif if (NULL == pUrl || NULL == request || NULL == response || NULL == pHttpResInfo) { return CURLE_FAILED_INIT; } res = curl_global_init(CURL_GLOBAL_ALL); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl global init fail and ret %d", res); return res; } curl = curl_easy_init(); if (NULL == curl) { res = CURLE_FAILED_INIT; HTTPS_LOG(LOG_LEVEL_ERROR, "curl init fail"); goto exit; } headers = curl_slist_append(headers, "Content-Type: application/json;charset=UTF-8"); if (NULL == headers) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl get header list fail"); goto exit; } #ifdef CLOUD_HTTPS_DEBUG //provide a buffer to store errors in res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_ERRORBUFFER ret %d", res); goto exit; } #endif if (IS_SESSION_DEBUG_ON()) { HTTPS_LOG(LOG_LEVEL_DEBUG, "post sesstion debug on"); res = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_VERBOSE ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_DEBUGFUNCTION ret %d", res); goto exit; } } res = curl_easy_setopt(curl, CURLOPT_URL, pUrl); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_URL ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_HTTPHEADER ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_POST, 1); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_POST ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_POSTFIELDS ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_READFUNCTION ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData_Post); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_WRITEFUNCTION ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_WRITEDATA ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_NOSIGNAL ret %d", res); goto exit; } if (IS_CA_PATH_NULL()) { res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_SSL_VERIFYPEER ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_SSL_VERIFYHOST ret %d", res); goto exit; } } else { res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_SSL_VERIFYPEER ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_CAINFO, GET_CA_PATH()); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_CAINFO ret %d", res); goto exit; } if (GET_CA_TYPE()) { res = curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,GET_CA_TYPE()); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_SSLCERTTYPE ret %d", res); goto exit; } } else { //curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM"); } } res = curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, GET_P_CONNECT_TIMEOUT()); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_CONNECTTIMEOUT ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_TIMEOUT, GET_P_TRANSFER_TIMEOUT()); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_TIMEOUT ret %d", res); goto exit; } res = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1); if (CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_ERROR, "curl set option CURLOPT_FOLLOWLOCATION ret %d", res); goto exit; } res = curl_easy_perform(curl); #ifdef CLOUD_HTTPS_DEBUG if(CURLE_OK != res) { HTTPS_LOG(LOG_LEVEL_DEBUG, "curl post return error: %s", errbuf); } #endif curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &(pHttpResInfo->status_code)); HTTPS_LOG(LOG_LEVEL_DEBUG, "cloud_https_post done. ret %d, http status code %ld", res, pHttpResInfo->status_code); exit: if (headers) { curl_slist_free_all(headers); } if (curl) { curl_easy_cleanup(curl); } curl_global_cleanup(); return res; }解析函数
10-29
int https_post(char *ip,char *auth_key_value,char *data,int port) { //https:// ${IP}:10010/probe/status CURL *curl=NULL; CURLcode res; struct curl_slist *headers=NULL; char url[100]="https://"; char port_str[7]={0}; //获取ip //char ip[16]={0}; strcat(url,ip); snprintf(port_str,7, ":%d",port); strncat(url,port_str,6); strncat(url,"/probe/status",13); //设置解析类型 headers=curl_slist_append(headers,"Content-Type:application/json"); char auth[100]="authentication_key:"; //获取密钥 长度最大32 //char auth_key_value[33]={0}; strcat(auth,auth_key_value); // 设置密钥 headers=curl_slist_append(headers,auth); //char data[]={0}; curl_global_init(CURL_GLOBAL_ALL); curl=curl_easy_init(); if(!curl){ return 0; } curl_easy_setopt(curl,CURLOPT_POST,1); curl_easy_setopt(curl,CURLOPT_URL,url); curl_easy_setopt(curl,CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl,CURLOPT_POSTFIELDS,data); curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0L); curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,0L); //超时时间为10秒 curl_easy_setopt(curl,CURLOPT_TIMEOUT, 10L); res=curl_easy_perform(curl); if(res != CURLE_OK) { switch (res) { case CURLE_COULDNT_CONNECT: fprintf(stderr,"res=%d can not connect remote host or proxy : %s \n",res,curl_easy_strerror(res)); break; case CURLE_HTTP_RETURNED_ERROR: fprintf(stderr,"res=%d http return error information : %s \n",res,curl_easy_strerror(res)); break; case CURLE_UNSUPPORTED_PROTOCOL: fprintf(stderr,"res=%d not support the protocol : %s \n",res,curl_easy_strerror(res)); break; default : fprintf(stderr,"res=%d curl_easy_perform() failed : %s \n",res,curl_easy_strerror(res)); } curl_slist_free_all(headers); curl_easy_cleanup(curl); curl_global_cleanup(); return 0; } curl_easy_perform卡死 curl_slist_free_all(headers); curl_easy_cleanup(curl); curl_global_cleanup(); return 1; } 执行到curl_easy_perform卡死
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值