When i am trying to do some HTTP_POST,HTTP_DELETE operation to a unreachable server the return code is sometimes coming as -1.Because of that my application is crashing.
Here is my sample code.
My application code is in c++ where i am reading the response and response header also.
lCode = curl_easy_setopt(curlHandle, CURLOPT_HEADERFUNCTION,
HttpClientImplCurl::recvFunctionHeader);
if (lCode != CURLE_OK) {
LOG(LOG_CRIT, "CURL failure: curl_easy_setopt CURLOPT_HEADERFUNCTION returned %d\n", lCode);
curl_slist_free_all(lHeaders);
errorBuff = curl_easy_strerror(lCode);
return http_code;
}
lCode = curl_easy_setopt(curlHandle, CURLOPT_WRITEHEADER,
&readResHeaderBuffer);
if (lCode != CURLE_OK) {
LOG(LOG_CRIT, "CURL failure: curl_easy_setopt CURLOPT_WRITEHEADER returned %d\n", lCode);
curl_slist_free_all(lHeaders);
errorBuff = curl_easy_strerror(lCode);
return http_code;
}
lCode = curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION,
HttpClientImplCurl::recvFunction);
if (lCode != CURLE_OK) {
LOG(LOG_CRIT, "CURL failure: curl_easy_setopt WRITEFUNCTION returned %d\n", lCode);
curl_slist_free_all(lHeaders);
errorBuff = curl_easy_strerror(lCode);
return http_code;
}
lCode = curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA,
&readResBuffer);
if (lCode != CURLE_OK) {
LOG(LOG_CRIT, "CURL failure: curl_easy_setopt WRITEDATA returned %d\n", lCode);
curl_slist_free_all(lHeaders);
errorBuff = curl_easy_strerror(lCode);
return http_code;
}
lCode = curl_easy_setopt(curlHandle, CURLOPT_CUSTOMREQUEST, "DELETE"); /* !!! */
if (lCode != CURLE_OK) {
LOG(LOG_CRIT, "CURL failure: curl_easy_setopt CURLOPT_CUSTOMREQUEST returned %d\n", lCode);
curl_slist_free_all(lHeaders);
errorBuff = curl_easy_strerror(lCode);
return http_code;
}
lCode = curl_easy_setopt(curlHandle, CURLOPT_POSTFIELDS, aInRemovedata.getBuffer()); /* data goes here */
if (lCode != CURLE_OK) {
LOG(LOG_CRIT, "CURL failure: curl_easy_setopt CURLOPT_POSTFIELDS returned %d\n", lCode);
curl_slist_free_all(lHeaders);
errorBuff = curl_easy_strerror(lCode);
return http_code;
}
lCode = curl_easy_perform(curlHandle);
if (lCode != CURLE_OK) {
LOG(LOG_CRIT, "CURL failure: curl_easy_perform returned %d\n", lCode);
curl_slist_free_all(lHeaders);
errorBuff = curl_easy_strerror(lCode);---->my doubt is when i am geting "-1", this line is caused the crash.
return http_code;
}
And my callback are as follows,
size_t
HttpClientImplCurl::recvFunctionHeader(void *aInBuf, size_t aInSize,
size_t aInNmemb, void* aInUserp)
{
fprintf(stderr, "curl error: too small buffer in recv2\n");
((std::string*)aInUserp)->append((char*)aInBuf, aInSize * aInNmemb);
return aInSize * aInNmemb;
}
// Private static callback
size_t
HttpClientImplCurl::recvFunction(void *aInBuf, size_t aInSize,
size_t aInNmemb, void* aInUserp)
{
fprintf(stderr, "curl error: too small buffer in recv1\n");
((std::string*)aInUserp)->append((char*)aInBuf, aInSize * aInNmemb);
return aInSize * aInNmemb;
}
Please help me on this, why we are getting occasionally "-1" in place of "28".
Here is the function signature, where errorBuf is coming as a parameter
int
HttpClientImplCurl::put(const HttpClient& aInClient,const base::Buffer& aInPutdata,
base::Buffer& aOutRecvBuf,base::Buffer& aOutRecvHeaderBuf,const char*& errorBuff)
您可以尝试设置CURLOPT_ERRORBUFFER和CURLOPT_VERBOSE以获取有关为什么curl_easy_perform返回的值为-1的更多信息。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.