Http status code with libcurl?

前端 未结 2 714
一整个雨季
一整个雨季 2020-12-04 17:29

How do I get the HTTP status code (eg 200 or 500) after calling curl_easy_perform?

相关标签:
2条回答
  • 2020-12-04 17:47

    The other answer is absolutely correct, but I would also like to add that it might not be wise to check the error code by hand, the 200 code is not the only code that signifies success.

    I'd recoment using the libcurl option CURLOPT_FAILONERROR that when activated will make libcurl consider 400 and 500 -category statuses a request failure and will not return CURLE_OK from perform.

    0 讨论(0)
  • 2020-12-04 18:05

    http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html

    CURLINFO_RESPONSE_CODE
    
    Pass a pointer to a long to receive the last received HTTP or FTP code. This
    option was known as CURLINFO_HTTP_CODE in libcurl 7.10.7 and earlier. This 
    will be zero if no server response code has been received. Note that a 
    proxy's CONNECT response should be read with CURLINFO_HTTP_CONNECTCODE 
    and not this. 
    
    curl_code = curl_easy_perform (session);
    long http_code = 0;
    curl_easy_getinfo (session, CURLINFO_RESPONSE_CODE, &http_code);
    if (http_code == 200 && curl_code != CURLE_ABORTED_BY_CALLBACK)
    {
             //Succeeded
    }
    else
    {
             //Failed
    }
    
    0 讨论(0)
提交回复
热议问题