Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default proxy in .net core 2.0

I saw couple of questions asked about core 2.0 on how to make HttpClient to use default proxy configured on the system. But no where found right answer. Posting this question hoping someone who might have encountered this issue might have found the solution by now.

In .net framework versions I've used the following configuration in my web.config and it worked for me.

  <system.net>
    <defaultProxy useDefaultCredentials="true"></defaultProxy>
  </system.net>

But in .net core 2.0 where I've make a web request to external api from my company's intranet my code is failing with 407, proxy authentication required.

After little bit of research I am of the opinion that it is not possible to make your HttpClient to use default proxy settings configured via WPAD in IE. Can someone correct my understanding here?

On this page of https://github.com/dotnet/corefx/issues/7037

It is said as follows :

"The default for HttpClientHandler.UseProxy property is true. And the default value of HttpClientHandler.Proxy is NULL which means to use the default proxy."

But I don't observe this behavior.

Update:

I am finally able to call external web api by specifying the proxy server address and then making the HttpClient call. Still wondering how to use default proxy setup in IE.

 using (var handler = new HttpClientHandler {
                    Credentials = new System.Net.NetworkCredential(user, password, domain),

                    UseProxy = true,
                    Proxy = new System.Net.WebProxy(new Uri("http://xxxxxxx:8080"), true)                                       
                })
{

    handler.Proxy.Credentials = new NetworkCredential("xxxx", "yyyyy", "cccccc");                    
    using (var httpClient = new HttpClient(handler))
    {
        var request = new HttpRequestMessage()
        {
            RequestUri = new Uri(destinationUrl),
            Method = HttpMethod.Post
        };

        request.Content = new StringContent(requestXml, Encoding.UTF8, "text/xml");             

        HttpResponseMessage response = await httpClient.SendAsync(request);

        Task<Stream> streamTask = response.Content.ReadAsStreamAsync();
    }
}   

If any one interested in finding out how I was able to find out the proxy server was, I wrote the following code in .net 4.0 and found out the proxy used.

var proxy = WebRequest.GetSystemWebProxy();
var url = proxy.GetProxy(new Uri("http://google.com"));

Thanks

like image 441
coolcake Avatar asked May 07 '18 01:05

coolcake


People also ask

What is the default proxy?

More Definitions of Default Proxy Default Proxy . (default option) means that currently selected Java Web Start (or Java Plug-in) proxy settings will be used.

What is proxy in asp net core?

ASP.NET Core in Action A reverse proxy is software responsible for receiving requests and forwarding them to the appropriate web server. The reverse proxy is exposed directly to the internet, whereas the underlying web server is exposed only to the proxy.

What is a proxy service C#?

Proxy is a structural design pattern that provides an object that acts as a substitute for a real service object used by a client. A proxy receives client requests, does some work (access control, caching, etc.) and then passes the request to a service object.

What is reverse proxy in .NET core?

A reverse proxy is a special type of proxy server that hides the target server to the client. The client requests a resource to the proxy server which retrieves it from another server and provides it to the client. In this case, the client has no idea that the resource comes from another server.


1 Answers

I hope this is the answer you're looking for: Default Proxy issues #28780

If you simply want to use the default system proxy and need to pass default credentials to that proxy (because the proxy is an authenticated proxy) during HTTP requests, then do this:

var handler = new HttpClientHandler();
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
var client = new HttpClient(handler);
like image 191
Vladislav Vitalyev Avatar answered Nov 02 '22 01:11

Vladislav Vitalyev