使用 libcurl 验证用户凭据
Verifying user credientials with libcurl
我使用以下代码的目标是使用 curl 来确定提供的 url
和 uidPwd
是否足以连接到提供的 URL。如果url错了,curl_easy_perform
returnsCANNOT_RESOLVE_HOST
。但是,如果 url 正常,则调用 returns CURLE_OK
,无论用户是否提供了有效凭据。我明白为什么会这样,curl 正在连接到提供的 url 而不管凭据是否正确(如果它们不正确而不是提供请求的资源它 returns 等于 "authentication failed")。我的问题是,有没有办法只使用 'curl_easy_perform' 的 return 代码就可以知道身份验证是否失败?如果没有,解决这个问题最简单的方法是什么?我不想去解析 returned 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);
我无法弄清楚如何单独使用 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;
}
我使用以下代码的目标是使用 curl 来确定提供的 url
和 uidPwd
是否足以连接到提供的 URL。如果url错了,curl_easy_perform
returnsCANNOT_RESOLVE_HOST
。但是,如果 url 正常,则调用 returns CURLE_OK
,无论用户是否提供了有效凭据。我明白为什么会这样,curl 正在连接到提供的 url 而不管凭据是否正确(如果它们不正确而不是提供请求的资源它 returns 等于 "authentication failed")。我的问题是,有没有办法只使用 'curl_easy_perform' 的 return 代码就可以知道身份验证是否失败?如果没有,解决这个问题最简单的方法是什么?我不想去解析 returned 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);
我无法弄清楚如何单独使用 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;
}