Detect proxy setting on webrequest

4

Good afternoon my dear, I need to make a request for an api that is in a domain outside the local network, I do this through a webrequest that was created this way:

WebRequest myWebRequest = WebRequest.Create("urldestino");

However, the network has a proxy, which blocked my requests stating that proxy authentication was required. To test the operation, I was able to perform the request by setting the proxy manually as follows:

WebProxy myProxy = new WebProxy();
myProxy = (WebProxy)WebProxy.GetDefaultProxy();
myProxy.Credentials = new NetworkCredential("meuusuario", "minhasenha", "meudominio");
myWebRequest.Proxy = myProxy;

Detail: After running "myProxy = (WebProxy)WebProxy.GetDefaultProxy();" myProxy.Credentials got null, so I set it manually.

In this way, the request passes through the proxy, however, I was directed to get the proxy session already authenticated by windows, in the same way that browsers and postman does, in postman for example I make the request without the need of inform the proxy data and it does not return me a proxy authentication is required.

Is there any way I can get the authenticated proxy session, without having to manually enter the user's registration and password?

Thank you very much in advance.

    
asked by anonymous 21.07.2017 / 22:33

1 answer

1

To avoid passing the credentials manually you can obtain the credentials as follows:

WebProxy myProxy = new WebProxy();
myProxy = (WebProxy)WebProxy.GetDefaultProxy();
myProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
myWebRequest.Proxy = myProxy;

With System.Net.CredentialCache.DefaultCredentials; it will return the credentials already set in IE by default, just below you arrow to webrequest.proxy the credentials.

    
24.07.2017 / 15:09