如何在libcurl中设置代理授权

时间:2014-12-03 12:05:45

标签: libcurl

实际上我正在尝试将一些数据发布到URL。但是我因为代理而陷入困境。 即使设置了值,下面的代码段也始终需要代理授权。 请告诉我代码中需要更改的任何内容。 这是我正在使用的代码片段

curl_easy_setopt(curl_handle, CURLOPT_PROXY, "proxy proxy.xyz.com:8080");
curl_easy_setopt(curl_handle, CURLOPT_PROXYTYPE, "CURLPROXY_HTTP");
curl_easy_setopt(curl_handle, CURLOPT_USERPWD, "abc/xyz:pwd");
curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, jsonResult);
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://username:password@url");
curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, error);

res = curl_easy_perform(curl_handle);

if(res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
return false;
}

我使用了CURLOPT_VERBOSE选项 以下是输出

* About to connect() to proxy proxy.xyz.com port 8080 (#0)
*   Trying proxy ip... * connected
* Server auth using Basic with user 'abc/xyz'
> POST http://username:password@url HTTP/1.1
Authorization: Basic aW5kaWEvNjY2NTU3Ok5vdkAyMDE0
Host:ip:8080
Accept: */*
Proxy-Connection: Keep-Alive
Content-Length: 53
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 53 out of 53 bytes
< HTTP/1.1 407 Proxy Authorization Required
< Date: Thu, 04 Dec 2014 05:17:15 GMT
< Via: 1.1 localhost.localdomain
< Proxy-Connection: keep-alive
< Cache-Control: no-store
< Content-Type: text/html
< Content-Language: en
< Proxy-Authenticate: Negotiate
< Proxy-Authenticate: NTLM
< Content-Length: 322
<
<HEAD><TITLE>Proxy Authorization Required</TITLE></HEAD>
<BODY BGCOLOR="white" FGCOLOR="black"><H1>Proxy Authorization Required</H1><HR>
<FONT FACE="Helvetica,Arial"><B>
Description: Authorization is required for access to this proxy</B></FONT>
<HR>
<!-- default "Proxy Authorization Required" response (407) -->
</BODY>
* Connection #0 to host proxy.xyz.com left intact

提前致谢

1 个答案:

答案 0 :(得分:1)

如果您仍然返回407,则表示代理拒绝了您使用的凭据(或方法)。

设置CURLOPT_VERBOSE并验证请求是否符合要求并仔细检查代理是否真的接受您发送的内容。

CURLOPT_PROXYTYPE不是字符串,因此上面显示的代码不起作用。事实上,代理类型HTTP是默认的,如果没有设置,那么你可以删除该行。

最后,与您的问题没有关系,但不建议使用CUSTOMREQUEST,因为POSTFIELDS已经暗示了POST。