2016-05-13 197 views
0

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

 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); 

回答

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; 
     }