curl-library
How to check Server Connection Status using curl_easy_getinfo
Date: Thu, 29 Jan 2015 15:02:47 +0530
Hi,
Is there way to just check the connection status of server? I need to know
the connection status of the server before posting the actual data. If
connection with server is available, then post the data to the server
otherwise dont post.
I have written the following function to check the connection: All the
required parameters are set to the curl handler before passing to this
function
int chkConn(CURL * curlHandle)
{
int rv = SUCCESS;
long lCode = 0L;
printf("%s: --- enter ---", __FUNCTION__);
while(1)
{
curl_easy_setopt(curlHandle, CURLOPT_POST, 0L);
curl_easy_setopt(curlHandle, CURLOPT_NOBODY, 1);
curl_easy_setopt(curlHandle, CURLOPT_FOLLOWLOCATION, 1);
rv = curl_easy_perform(curlHandle);
if(rv == CURLE_OK)
{
printf("%s: Perform SUCCESS", __FUNCTION__);
}
else
{
printf("%s: Return Value from PERFORM = [%d]",__FUNCTION__, rv);
}
rv = curl_easy_getinfo(curlHandle, CURLINFO_RESPONSE_CODE, &lCode);
printf(szDbgMsg, "%s: Return Value RESPONSE CODE = [%d],
[%ld]",__FUNCTION__, rv, lCode);
if(rv == 0 && lCode == 200)
{
printf("%s: Got Successful respcode from host",__FUNCTION__);
rv = SUCCESS;
}
else
{
printf("%s: Didnt get the good resp code from host", __FUNCTION__);
rv = FAILURE;
}
break;
}
printf("%s: Returning [%d]", __FUNCTION__, rv);
return rv;
}
I am testing this function with various servers to which i need to post the
data. one server was returning CURLE_OK for curl_easy_perform function and
CURLINFO_RESPONSE_CODE as 200 (which i considered as success), but one more
server was returning 22(CURLE_HTTP_RETURNED_ERROR) for curl_easy_perform
function and CURLINFO_RESPONSE_CODE as 404.
I tried to use CURLINFO_HTTP_CONNECTCODE instead of CURLINFO_RESPONSE_CODE
with the second server, i have received 0 for the
CURLINFO_HTTP_CONNECTCODE. I am not sure whether this value can be
considered as success.
Could you please help me to make it generic to test the connection status
of the server?
Thank you for your time and help.
regards
Praveen
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2015-01-29