【问题标题】:Verifying user credientials with libcurl使用 libcurl 验证用户凭证
【发布时间】:2016-05-13 17:09:54
【问题描述】:

我使用以下代码的目标是使用 curl 来确定提供的 urluidPwd 是否足以连接到提供的 URL。如果 url 错误,curl_easy_perform 返回CANNOT_RESOLVE_HOST。但是,如果 url 正常,则无论用户是否提供有效凭据,调用都会返回 CURLE_OK。我理解为什么会发生这种情况,无论凭据是否正确,curl 都会连接到提供的 url(如果它们不正确而不是提供请求的资源,它会返回相当于“身份验证失败”的内容)。我的问题是,有没有办法知道身份验证是否失败,只使用'curl_easy_perform'的返回码?如果没有,解决这个问题的最简单方法是什么?我不想麻烦解析返回的HTTP。

        curl_easy_setopt(curlHandle, CURLOPT_URL, url);
        curl_easy_setopt(curlHandle, CURLOPT_USERPWD, uidPwd);
        curl_easy_setopt(curlHandle, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);

        curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, &BJConnection::writefunc);
        curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &s);

        returnCode = curl_easy_perform(curlHandle);
        assert(returnCode == CURLE_OK);

【问题讨论】:

    标签: c++ curl libcurl


    【解决方案1】:

    我无法弄清楚如何单独使用 libcURL 来做到这一点。但是,我正在使用 PugiXML 来解析 libcURL 返回的结果,并找到了一种解决方法来使用 Pugi 检查所提供凭据的有效性。这是解决方案:

            returnCode = curl_easy_perform(curlHandle);
    
            pugi::xml_document doc;
            // We will only be able to load the result into Pugi XML if libcURL
            // returns XML. HTTP is returned in the event of a login error
            pugi::xml_parse_result loginSuccess = doc.load_string(s.ptr);
            if (returnCode != CURLE_OK) {
                BJTHROWGEN("Error connecting to Bamboo server. cURL return code: " +
                    returnCode);
            } else if (!loginSuccess) {
                BJTHROWGEN("Could not login to Bamboo using the provided credientials.");
            }
            else {
                // Success
                m_isConnected = true;
            }
    

    【讨论】:

      猜你喜欢
      • 2013-12-28
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      相关资源
      最近更新 更多