代理

时间:2017-05-19 14:42:32

标签: c++ curl proxy libcurl

只是一个简单的文件下载测试。不工作。 Probaby因为公司的代理人。如何配置代理? 在添加“127.0.0.1:8080”之前,我总是遇到错误7:无法连接到服务器。现在我总是得到返回代码0,但测试文件json.txt只包含404.但是网址显然在我的浏览器中工作。我还尝试通过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);
}

更新

Internet设置配置为使用wpad.dat文件来检测代理。如果我输入其中一个,我从服务器得到一个响应,但它说访问被拒绝,因为网站被阻止。但是我的浏览器可以显示这个网站。

相反,如果我输入netsh.exe winhttp show proxy,则表示当前的winhttp代理设置是“直接访问(无代理服务器)”。

更新2

我终于使用了这些选项:使用Verbose获得更多有用的输出;输出显示了一些身份验证方法,如negotiatentlm;如果libcurl具有sspi模块,则ntlm选项不起作用,但negotiate选项与userpwd :一起从系统获取凭据。

    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, ":" );

1 个答案:

答案 0 :(得分:0)

尝试用户代理并定义标题。

#define HEADER_ACCEPT "Accept:text/html,application/xhtml+xml,application/xml"
#define HEADER_USER_AGENT "User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.70 Safari/537.17"

struct curl_slist *list = NULL;
list = curl_slist_append(list, HEADER_USER_AGENT);
list = curl_slist_append(list, HEADER_ACCEPT);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);

您可能需要真正的代理,找到代理网站,然后使用其他IP重试