0

使用 libcurl 版本:7.19.7

[对不起,这条消息的冗长]

我在获取简单的基本身份验证以使用 libcurl 时遇到问题。我有一个服务器设置来提供受密码保护的数据:url 在代码中。

我采用了 simple.c libcurl 示例并将其修改为设置 CURLOPT_USERPWD(代码如下)。当我运行它时,我在 [html styles elided] 下面得到 curl 详细输出。

笔记:

  1. 如果我将 url 粘贴到浏览器中,它会询问用户和密码,如果提供,它会正确返回数据。我认为这意味着服务器声称未经授权是虚假的(对吗?)。
  2. 如果我将“用户名:passwd@”放入 url,我似乎得到了相同的结果。

有人看到问题了吗?

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

2 回答 2

1

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

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

告诉 libcurl为您的会话启用 cookie

...
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies.txt");
...
res = curl_easy_perform(curl);
于 2013-11-12T19:55:36.387 回答
0

在您的粘贴中,我们看到 curl 如何发送

授权:基本 dGlja2V0OnRpY2tldDE=

...到我们可以base64解码的服务器,并看到您使用基本身份验证将“ticket:ticket1”作为用户和密码发送。服务器用 401 拒绝它。

我不明白这是 curl 的错...

于 2013-11-12T22:17:15.483 回答