【发布时间】:2017-10-19 18:06:11
【问题描述】:
只是一个简单的文件下载测试。不工作。可能是因为公司的代理人。如何配置代理? 在添加“127.0.0.1:8080”之前,我总是收到错误 7:无法连接到服务器。现在我总是得到返回码 0,但测试文件 json.txt 只包含一个 404。但 url 显然在我的浏览器中工作。我也尝试通过this从系统获取代理设置,但失败了。
HTTP/1.1 404 Not Found
Connection: Keep-Alive
Server: Embedthis-http
ETag: "0-0-56d6b6ea"
Cache-Control: no-cache
Last-Modified: Wed, 02 Mar 2016 09:48:26 GMT
Date: Fri, 19 May 2017 14:33:57 GMT
Content-Length: 167
Keep-Alive: timeout=60, max=199
<!DOCTYPE html>
<html><head><title>Not Found</title></head>
<body>
<h2>Access Error: 404 -- Not Found</h2>
<pre>Cannot locate document: /</pre>
</body>
</html>
这是我的 cURL 尝试:
CURL *curl;
FILE *fp;
char url[] = "http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getMakes&year=2010&sold_in_us=1";
char outfilename[FILENAME_MAX] = "C:\\temp\\json.txt";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, url );
curl_easy_setopt(curl, CURLOPT_NOBODY, true);
curl_easy_setopt(curl, CURLOPT_HEADER, true);
curl_easy_setopt(curl, CURLOPT_HTTPGET, true);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_easy_setopt(curl, CURLOPT_PROXY, "127.0.0.1:8080" );
res = curl_easy_perform(curl);
cout << "Curl response code " << res << ": " << curl_easy_strerror( res ) << endl;
curl_easy_cleanup(curl);
fclose(fp);
}
更新
互联网设置被配置为使用wpad.dat 文件来检测代理。如果我输入其中之一,我会收到来自服务器的响应,但它说访问被拒绝,因为该网站被阻止。但是我的浏览器可以显示这个网站。
相反,如果我输入netsh.exe winhttp show proxy,则表示当前的winhttp代理设置为“直接访问(无代理服务器)”。
更新 2
我终于可以使用这些选项:使用Verbose 获得更多有用的输出;输出显示了一些身份验证方法,例如negotiate 和ntlm; ntlm 选项不起作用,但 negotiateone 与 userpwd : 一起从系统获取凭据,如果 libcurl 具有 sspi 模块。
curl_easy_setopt(curl, CURLOPT_URL, url );
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1 );
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY );
curl_easy_setopt(curl, CURLOPT_PROXY, <"host:port"> );
curl_easy_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_NEGOTIATE );
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, ":" );
【问题讨论】: