Proxy connection: HTTP 407 Proxy authentication Required

Asked

Viewed 4,326 times

7

I created a project for ASP NET where I want to get the data of an address from the zip code by calling the url on a webservice.

On my machine works normally, but on the machine I work, there is a need for network authentication. When I give the Submit in the form, the exception is triggered with the following message:

Remote server returned an error: (407) Proxy Authentication Required.

Below is the code I tried to use to make the connection from other Stackoverflow gringa solutions, but without success:

WebClient webClient = new WebClient();
webClient.Proxy = new WebProxy("XX.XX.XX.XXX", 999);
webClient.Credentials = new NetworkCredential("XXXXX", "*********");

I appreciate the help!

2 answers

6


I usually set proxy with . NET as follows:

I create a class that inherits from Iwebproxy:

namespace MinhaEmpresa.MeuProduto.Web.Proxy {
    public class MyProxy : IWebProxy
    {
        public ICredentials Credentials
        {
            get
            {               
                return new NetworkCredential("USUARIO", "UMA_SENHA_SEGURA");
            }
            set { }
        }

        public Uri GetProxy(Uri destination)
        {           
            return new Uri("http://host:port");
        }

        public bool IsBypassed(Uri host)
        {       
            return false;
        }

    }
}

In the application’s web.config, you must configure the proxy created:

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="false">
        <module type="MinhaEmpresa.MeuProduto.Web.Proxy.MyProxy, MinhaEmpresa.MeuProduto.Web"/>
    </defaultProxy>
    <settings>
        <servicePointManager expect100Continue="false" />
    </settings>
</system.net>

The parameters of the type attribute of the module tag are, in that order:

  • Name of the implementing class IWebProxy (in the example MinhaEmpresa.MeuProduto.Web.Proxy.MyProxy)
  • Assembly name that the class that implements Iwebproxy is contained (DLL name without the extension).

The advantage of this solution is that proxy settings can be anywhere: in a database, on . config of the application, in an XML, etc. I suggest not to leave them Hard Code as in the example.

  • Together with this answer: http://codigosfontes.blogspot.com.br/2011/05/obtendo-o-html-de-uma-pagina-com-c.html and this: http://stackoverflow.com/questions/954635/the-servicepointmanager-does-not-support-proxies-of-scheme your solution was simple and solved my problem. However I could not obtain the data using the Webclient object, theoretically more practical, stating that an error occurred in Webclient(); Know something about it?

  • 1

    You could not run without the last function because you are implementing an interface (contract), when you do this you are required to write the methods it declares. Read more on https://msdn.microsoft.com/en-us/library/87d83y5b.aspx and https://msdn.microsoft.com/en-us/library/ms173156.aspx. I will edit out the answer to explain better the .config.

  • As for the Webclient, I suggest opening another question with your specific problem.

0

I had exactly the same error as user21846, in my house the code worked normally, but in the service presented the following error message.

Remote server returned an error: (407) Proxy Authentication Required.

But solved in a simpler way, I added the following excerpt in the file "App.config"

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

Note: Pasted inside the tag "Configuration"

So the code ran just like in my house, with this snippet it just takes the default proxy from the machine.

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.