Proxy Connection: HTTP 407 Proxy Authentication Required

6

I created a project for ASP NET where I want to get the data of an address from the CEP by querying a call from the url in a webservice.

On my machine it works normally, however on the machine I work, there is a need for network authentication. When I submit the submit in form, the exception is thrown with the following message:

  

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

Below is the code I tried to use to connect from other StackOverflow gringas, but to no avail:

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

Thanks for the help!

    
asked by anonymous 28.04.2015 / 19:23

1 answer

6

I usually configure 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 class that implements IWebProxy (in example MinhaEmpresa.MeuProduto.Web.Proxy.MyProxy )
  • Name of the assembly that the class that implements IWebProxy is contained (name of the DLL, without the extension).

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

    
28.04.2015 / 23:30