CURLOPT_USERPWD似乎被忽略了

时间:2013-11-12 19:24:39

标签: libcurl basic-authentication

使用libcurl版本:7.19.7

[抱歉此消息的详细程度]

我遇到了简单的Basic问题 身份验证与libcurl一起使用。 我有一个服务器设置来提供密码 受保护的数据:url在代码中。

我使用了simple.c libcurl示例 并修改它以设置CURLOPT_USERPWD (以下代码)。当我运行它时,我得到了 curl verbose输出[html styles elided]。

注意:

  1. 如果我将网址粘贴到浏览器中,它会    询问用户和密码,如果提供    它正确返回数据。我假设这个    意味着服务器声称存在    未经授权是假的(对吗?)。
  2. 如果我将“username:passwd @”放入网址,我就会出现    得到相同的结果。

有人看到问题吗?

= Dennis Heimbigner Unidata的

卷曲详细输出

* About to connect() to utmea.enea.it port 8080 (#0)
*   Trying 192.107.77.41... * connected
* Connected to utmea.enea.it (192.107.77.41) port 8080 (#0)
> > GET /thredds/dodsC/UNIDATA_passwd/head_out.nc.dds HTTP/1.1
Host: utmea.enea.it:8080
Accept: */*

< HTTP/1.1 307 Temporary Redirect
< Server: Apache-Coyote/1.1
< Last-Modified: 
< Set-Cookie: JSESSIONID=BEC21BBB6DD954B7BD60F1BED1414A8E; Path=/thredds/; HttpOnly
< Location: http://utmea.enea.it:8080/thredds/restrictedAccess/accediUNIDATA
< Content-Length: 0
< Date: Tue, 12 Nov 2013 18:49:00 GMT
< 
* Connection #0 to host utmea.enea.it left intact
* Issue another request to this URL: 'http://utmea.enea.it:8080/thredds/restrictedAccess/accediUNIDATA'
* Re-using existing connection! (#0) with host utmea.enea.it
* Connected to utmea.enea.it (192.107.77.41) port 8080 (#0)
> > GET /thredds/restrictedAccess/accediUNIDATA HTTP/1.1
Host: utmea.enea.it:8080
Accept: */*

< HTTP/1.1 401 Unauthorized
< Server: Apache-Coyote/1.1
< Cache-Control: private
< Expires: Thu, 01 Jan 1970 01:00:00 CET
< WWW-Authenticate: Basic realm="THREDDS Data Server"
< Content-Type: text/html;charset=utf-8
< Content-Length: 951
< Date: Tue, 12 Nov 2013 18:49:00 GMT
< 
* Ignoring the response-body
* Connection #0 to host utmea.enea.it left intact
* Issue another request to this URL: 'http://utmea.enea.it:8080/thredds/restrictedAccess/accediUNIDATA'
* Re-using existing connection! (#0) with host utmea.enea.it
* Connected to utmea.enea.it (192.107.77.41) port 8080 (#0)
* Server auth using Basic with user 'ticket'
> > GET /thredds/restrictedAccess/accediUNIDATA HTTP/1.1
Authorization: Basic dGlja2V0OnRpY2tldDE=
Host: utmea.enea.it:8080
Accept: */*

< HTTP/1.1 401 Unauthorized
< Server: Apache-Coyote/1.1
< Cache-Control: private
< Expires: Thu, 01 Jan 1970 01:00:00 CET
< Set-Cookie: JSESSIONID=E0E3AC390A39C786C3CFD139F601D8B8; Path=/thredds/; HttpOnly
< Content-Type: text/html;charset=utf-8
< Content-Length: 1027
< Date: Tue, 12 Nov 2013 18:49:00 GMT
< 
<html><head><title>Apache Tomcat/7.0.35 - Error report</title>
<style>...</style>
</head><body><h1>HTTP Status 401 - Not authorized to access this dataset.</h1>
... not authorized to access this dataset.</u></p><p><b>description</b> <u>This request requires HTTP authentication.</u></p>...<h3>Apache Tomcat/7.0.35</h3></body></ht* Connection #0 to host utmea.enea.it left intact
* Closing connection #0

修改后的simple.c

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://utmea.enea.it:8080/thredds/dodsC/UNIDATA_passwd/head_out.nc.dds");
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_easy_setopt(curl, CURLOPT_USERPWD, "ticket:ticket1");

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}

2 个答案:

答案 0 :(得分:1)

  

如果我将网址粘贴到浏览器[...]中,它会正确返回数据。我认为这意味着服务器声称未经授权是虚假的(正确吗?)。

否:这是因为浏览器支持cookie(这是此Web服务所期望的)。

告诉libcurl to enable cookies for your session

...
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
...
res = curl_easy_perform(curl);

答案 1 :(得分:0)

在您的粘贴中,我们会看到curl发送的方式

  

授权:基本dGlja2V0OnRpY2tldDE =

...到我们可以base64解码的服务器,看到你使用Basic auth发送“ticket:ticket1”作为用户和密码。并且服务器拒绝使用401。

我不明白这是怎么回事......