bvb
bvb

Reputation: 783

How to set proxy authorization in libcurl

Actually I am trying to post some data to the URL.But I am getting stuck because of proxy. The below code snippet is always giving proxy authorization required even though setting the values. Please let me know any thing needs to be changed in the code. Here is the code snippet that I am using

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

I have used the CURLOPT_VERBOSE option Below is the output

* 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

Thanks in advance

Upvotes: 2

Views: 1227

Answers (1)

Daniel Stenberg
Daniel Stenberg

Reputation: 58004

If you still get a 407 back, it means the proxy rejected the credentials (or method) you used.

Set CURLOPT_VERBOSE and verify that the request looks like you want it and double-check that the proxy really accepts what you send it.

CURLOPT_PROXYTYPE is not a string so the code you shown above will not work. In fact, proxy type HTTP is default if not set so you can just remove that line.

Finally, not related to your problem, but using CUSTOMREQUEST like that is not recommended since POSTFIELDS already implies a POST.

Upvotes: 2

Related Questions