使用libcurl

时间:2016-05-13 17:09:54

标签: c++ curl libcurl

我使用下面代码的目标是使用curl来确定提供的urluidPwd是否足以连接到提供的URL。如果网址错误,curl_easy_perform会返回CANNOT_RESOLVE_HOST。但是,如果网址正常,则无论用户是否提供有效凭据,呼叫都会返回CURLE_OK。我理解为什么会发生这种情况,curl正在连接到提供的URL,无论凭据是否正确(如果它们不正确而不是提供所请求的资源,它返回的数量为"认证失败&# 34)。我的问题是,有没有办法知道验证失败只使用' 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);

1 个答案:

答案 0 :(得分:0)

我无法单独使用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;
        }