2013-11-22 63 views
0

當我嘗試對一個無法訪問的服務器執行一些HTTP_POST,HTTP_DELETE操作時,返回代碼有時會變成-1.因爲我的應用程序崩潰了。當服務器無法訪問時,curl_easy_perform返回-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) 
+0

'errorBuff'是如何定義的? – alk

回答

0

你可以嘗試設置CURLOPT_ERRORBUFFER和CURLOPT_VERBOSE獲得爲什麼由curl_easy_perform返回的值是-1更多的信息。