[c#] The remote server returned an error: (407) Proxy Authentication Required

I'm getting this error when I call a web service:

"The remote server returned an error: (407) Proxy Authentication Required".

I get the general idea and I can get the code to work by adding

myProxy.Credentials = NetworkCredential("user", "password", "domain");

or by using DefaultCredentials in code. My problem is that the call to the web service works in production without this.

It seems like there is a non code solution involving Machine.config, but what is it? At the moment I can't get to the production box's machine.config file to see what that looks like. I tried updating my machine.config as follows, but I still get the 407 error.

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
        <bypasslist>
            <clear />
        </bypasslist>
        <proxy proxyaddress="myproxy:9000"
               usesystemdefault="false"
               bypassonlocal="true"
               autoDetect="False" />
    </defaultProxy>
</system.net>

The answer is


Check with your firewall expert. They open the firewall for PROD servers so there is no need to use the Proxy.

Thanks your tip helped me solve my problem:

Had to to set the Credentials in two locations to get past the 407 error:

HttpWebRequest webRequest = WebRequest.Create(uirTradeStream) as HttpWebRequest;
webRequest.Proxy = WebRequest.DefaultWebProxy;
webRequest.Credentials = new NetworkCredential("user", "password", "domain");
webRequest.Proxy.Credentials = new NetworkCredential("user", "password", "domain");

and voila!


I had a similar proxy related problem. In my case it was enough to add:

webRequest.Proxy.Credentials = new NetworkCredential("user", "password", "domain");

Probably the machine or web.config in prod has the settings in the configuration; you probably won't need the proxy tag.

<system.net>
    <defaultProxy useDefaultCredentials="true" >
        <proxy usesystemdefault="False"
               proxyaddress="http://<ProxyLocation>:<port>"
               bypassonlocal="True"
               autoDetect="False" />
    </defaultProxy>
</system.net>

HttpWebRequest webRequest = WebRequest.Create(uirTradeStream) as HttpWebRequest;

webRequest.Proxy = WebRequest.DefaultWebProxy;

webRequest.Credentials = new NetworkCredential("user", "password");

webRequest.Proxy.Credentials = new NetworkCredential("user", "password");

It is successful.


Just add this to config

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

In following code, we don't need to hard code the credentials.

service.Proxy = WebRequest.DefaultWebProxy;
service.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
service.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

Thought of writing this answer as nothing worked from above & you don't want to specify proxy location.

If you're using httpClient then consider this.

HttpClientHandler handler = new HttpClientHandler();

IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
handler.Proxy = proxy;

var client = new HttpClient(handler);
// your next steps...

And if you're using HttpWebRequest:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri + _endpoint);

IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
request.Proxy = proxy;

Kind referencce: https://medium.com/@siriphonnot/the-remote-server-returned-an-error-407-proxy-authentication-required-86ae489e401b


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to proxy-authentication

Visual Studio Error: (407: Proxy Authentication Required) The remote server returned an error: (407) Proxy Authentication Required

Examples related to http-status-code-407

Proxy Basic Authentication in C#: HTTP 407 error The remote server returned an error: (407) Proxy Authentication Required

Examples related to machine.config

Connection Strings for Entity Framework The remote server returned an error: (407) Proxy Authentication Required