Detect proxy configuration in webrequest

Asked

Viewed 528 times

4

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

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

However, the network has a proxy, which barred my requests informing that proxy authentication was necessary. To test the operation, I was able to perform the request by manually setting the proxy in the following way:

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

Detail: after the execution of "myProxy = (WebProxy)WebProxy.GetDefaultProxy();" myProxy.Credentials was null, so I manually set.

In this way, the request goes through the proxy, but I was directed to get the proxy session already authenticated by windows, in the same way that browsers and Postman do, in Postman for example I perform the request without the need to inform the proxy data and it does not return me a proxy Authentication is required.

Is there any way I can get the proxy session authenticated without having to manually pass the registration and password of the logged in users?

From now on I thank you very much.

  • The proxy is configured in Windows and is default?

  • I believe so, it is set in the browser to "use automatic configuration script" and below is set its address.

  • So you don’t need any code, the default of Webrequest is to use the proxy configured in the OS. I will not put as an answer because I have no way to test it now, but test there and leave a feedback in the comments =D

  • It is not getting automatic, I believe it would get in myProxy = (Webproxy)Webproxy.Getdefaultproxy(); however myproxy does not receive the credentials already configured, and by not passing the credentials I received a "{"The remote server returned an error: (407) Proxy Authentication Required."}"

1 answer

1


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

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

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

  • It worked as needed, authenticated in the proxy successfully getting the credentials without having to pass the user data. Thank you very much.

Browser other questions tagged

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