当服务器无法访问时,curl_easy_perform返回-1

时间:2013-11-22 15:03:29

标签: c++ c curl libcurl

当我尝试对无法访问的服务器执行HTTP_POST,HTTP_DELETE操作时,返回代码有时会变为-1。因为我的应用程序崩溃了。

这是我的示例代码。

我的应用程序代码在c ++中,我也在阅读响应和响应标题。

  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;
    }

我的回调如下,

  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;
}

请帮助我,为什么我们偶尔会“-1”代替“28”。

这是函数签名,其中errorBuf作为参数

 int
HttpClientImplCurl::put(const HttpClient& aInClient,const base::Buffer& aInPutdata,
                          base::Buffer& aOutRecvBuf,base::Buffer&  aOutRecvHeaderBuf,const char*& errorBuff)

1 个答案:

答案 0 :(得分:0)

您可以尝试设置CURLOPT_ERRORBUFFER和CURLOPT_VERBOSE以获取有关curl_easy_perform返回的值为-1的原因的更多信息。