如何在調用curl_easy_perform後獲取HTTP狀態碼(例如200或500)?帶有libcurl的http狀態碼?
76
A
回答
113
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
}
1
對方回答是絕對正確的,但我也想補充一點,它可能不是明智的手工檢查錯誤代碼,該200
代碼不是唯一代表成功的代碼。
我recoment使用的libcurl選項CURLOPT_FAILONERROR在被激活時將考慮的libcurl和400
500
-category狀態的請求失敗,而且不會從執行返回CURLE_OK
。
相關問題
- 1. libcurl http驗證碼
- 2. 如何發送帶有輸入法的HTTP狀態碼?
- 3. Response.Redirect HTTP狀態碼
- 4. HTTP狀態代碼
- 5. System.Net.WebException HTTP狀態碼
- 6. HTTP 1xx狀態碼
- 7. http狀態碼200
- 8. HTTP狀態碼406
- 9. SimpleXMLElement - HTTP狀態碼?
- 10. 「有錯誤成功」的HTTP狀態碼?
- 11. 有限集合的HTTP狀態碼?
- 12. PUT的HTTP狀態代碼
- 13. NotFoundException的HTTP狀態碼500
- 14. REST的4xx http狀態碼
- 15. GWT Requestfactory的HTTP狀態碼
- 16. 帶有狀態碼問題的TestLink APIException
- 17. HTTP狀態代碼406,沒有其他
- 18. 獲取有關HTTP狀態代碼
- 19. symfony2/FOSRestBundle:沒有HTTP狀態碼
- 20. 是否HTTP狀態碼事
- 21. 閱讀http狀態碼fsockopen
- 22. HTTP狀態代碼:在GCMDemo
- 23. 蟒蛇HTTP狀態代碼
- 24. 設置http狀態代碼
- 25. 角$ http和狀態碼304
- 26. Flash和HTTP狀態碼207
- 27. 開關HTTP狀態代碼
- 28. HTTP狀態碼200 vs 202
- 29. Python urllib2 URLError HTTP狀態碼。
- 30. HTTP狀態代碼413
好問題。另一個可能是,如何獲得狀態消息.. :) – mykhal 2010-04-27 17:07:12