Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate against a proxy when using the HttpClient class?

I have a console app built on .NET 4 that uses the HttpClient library (obtained via NuGet) to retrieve data from a public API over the internet. The console app sits behind a proxy. The Windows machine it sits on has the correct proxy settings in Internet Explorer. When the console application attempts to access the outside world it is given a 407 status - "proxy authentication required".

I've written no specific code to deal with a proxy yet. I'm assuming from the 407 error that the application is directing to the proxy no problem, but the proxy isn't authenticating the request.

My question is, is it possible to set the HttpClient to use the credentials of the logged in user to authenticate the proxy? If so, how?

var client = new HttpClient(webRequestHandler);
client.PostAsync(RequestUri, MyContent);
like image 547
Sio Avatar asked Mar 22 '12 18:03

Sio


People also ask

What is Httpclient proxy?

Advertisements. A Proxy server is an intermediary server between the client and the internet.


2 Answers

I managed to solve my problem very simply through proxy configuration in app.config.

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
        <proxy usesystemdefault="True" />
    </defaultProxy>
</system.net>

http://msdn.microsoft.com/en-us/library/dkwyc043.aspx

like image 103
Sio Avatar answered Oct 14 '22 21:10

Sio


If you wanted to do the same programmatically you could use the following:

WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;

(These objects are from the `System.Net' namespace).

Some more info from MSDN: http://msdn.microsoft.com/en-us/library/system.net.webproxy.getdefaultproxy.aspx

like image 31
Jaans Avatar answered Oct 14 '22 22:10

Jaans