2008-11-14 17 views
76

Wie bekomme ich den HTTP-Statuscode (zB 200 oder 500) nach dem Aufruf von curl_easy_perform?Http Statuscode mit libcurl?

+7

gute Frage. ein anderer könnte sein, wie man eine Statusmeldung bekommt .. :) – mykhal

Antwort

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

Die andere Antwort ist absolut richtig, aber ich möchte auch hinzufügen, dass es nicht klug, könnte den Fehlercode von Hand zu überprüfen, die 200 Code ist nicht der einzige Code, der Erfolg bedeutet.

Ich würde recoment die Libcurl Option CURLOPT_FAILONERROR, dass, wenn Libcurl betrachten machen 400 und 500 -Niveau Status ein Anforderungsfehler aktiviert wird und wird nicht zurückkehren CURLE_OK aus durchführen.