使用 libcurl 版本:7.19.7
[对不起,这条消息的冗长]
我在获取简单的基本身份验证以使用 libcurl 时遇到问题。我有一个服务器设置来提供受密码保护的数据:url 在代码中。
我采用了 simple.c libcurl 示例并将其修改为设置 CURLOPT_USERPWD(代码如下)。当我运行它时,我在 [html styles elided] 下面得到 curl 详细输出。
笔记:
- 如果我将 url 粘贴到浏览器中,它会询问用户和密码,如果提供,它会正确返回数据。我认为这意味着服务器声称未经授权是虚假的(对吗?)。
- 如果我将“用户名: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;
}