【问题标题】:Curl Error code 7卷曲错误代码 7
【发布时间】:2015-12-12 05:28:02
【问题描述】:

我在某些客户端计算机上遇到 curl 请求问题。尽管 http 请求在浏览器和大多数 win 7 64 位 sp1 机器上都有效,但在某些用户机器上却失败了。 感谢您的意见。这是使用 curl 的代码存根。

谢谢

bool HttpClient::request(const ArStr& url, HttpBuffer& header, HttpBuffer& body, unsigned long& status, ArStr& contentEncoding
    , bool bFollowRedirect /*= true*/, bool bForceNoCache /*= false*/, bool bForceNoCookie /*= true*/)
{
    try
    {
        bool ret = false;

        CURL *curl_handle;

        /* init the curl session */
        curl_handle = curl_easy_init();
        if (curl_handle == NULL)
        {
            Logger::Instance()->Error(_T("Failed to init curl"));
            return ret;
        }

        curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1);

        curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());

        unsigned long nConnectTimeout = _nConnectTimeout;

        if (bFollowRedirect)
            curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);

        if (bForceNoCookie)
            curl_easy_setopt(curl_handle, CURLOPT_COOKIESESSION, 1); // Set this value to ensure the session does not reuse old cookie, by default it is 0

        if (bForceNoCache)
            curl_easy_setopt(curl_handle, CURLOPT_FRESH_CONNECT, 1); // By default it is 0

        //add proxy setting with scheme

        char strProxy[1024] = { 0 };
       curl_easy_setopt(curl_handle, CURLOPT_PROXYPORT, strProxy);

        curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT_MS, &nConnectTimeout);

        unsigned long nTimeout = max(_nReceiveTimeout, _nSendTimeout);
        curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, &nTimeout);
        struct curl_slist *list = NULL;
        list = curl_slist_append(list, "Test: data");
        curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);
        /* no progress meter please */
        curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1);

        /* send all data to this function  */
        curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

        curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, &header);

        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &body);

        curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L);
        curl_easy_setopt(curl_handle, CURLOPT_DEBUGFUNCTION, debug_callback);
        curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1); // For outputing debug information
        curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
        /* get it! */
        CURLcode res = CURLE_OK;
        try
        {
            res = curl_easy_perform(curl_handle);
        }
        catch (exception ex)
        {
            Logger::Instance()->Error(_T("Failed to post data to %s, exception: %s"), url.c_str(), ex.what());
            res = CURLE_HTTP_POST_ERROR;
        }
        Logger::Instance()->Info(_T("status %d"), status);
        Logger::Instance()->Info(_T("curl result %d"), res);

        curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &status);

        char *ct;
        curl_easy_getinfo(curl_handle, CURLINFO_CONTENT_TYPE, &ct);
        if (ct != NULL)
            contentEncoding = Utils::StringToArStr(ct);

        _lastInetErrorCode = res;

        if (res != CURLE_OK)
        {
            Logger::Instance()->Error(_T("Failed to send HTTP request, error code: %d"), res);
            ret = false;
        }
        else
        {
            ret = true;
        }

        /* cleanup curl stuff */
        curl_easy_cleanup(curl_handle);

        return ret;
    }
    catch (exception ex)
    {
        Logger::Instance()->Error(_T("Failed to send http request"), ex.what());
    }
}

【问题讨论】:

    标签: curl libcurl


    【解决方案1】:

    List of return codes of cURL

    CURLE_COULDNT_CONNECT (7)

    无法连接()到主机或代理。

    【讨论】:

    • 我已经查过了这个错误代码。但是为什么会失败(浏览器上的http请求有效)
    • @PriyaRajput 防火墙?网络超时?同一个专用网络?一些奇怪的窗口安全?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 2015-04-18
    相关资源
    最近更新 更多