gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:43:56 27 4
gpt4 key购买 nike

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

这是我的示例代码。

我的应用程序代码是用 C++ 编写的,我也在其中读取响应和响应 header 。

  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)

最佳答案

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

关于c++ - 当服务器无法访问时,curl_easy_perform 返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20147975/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com