How can I get a new default WebProxy object in a C # Windows Store app?

My question is related to a problem that I discovered on the corporate network. I am developing Windows 8, so in my case IE10 is configured to automatically detect proxy settings.

In my C # application, I am using System.Net.Http.HttpClient. I found that the default IWebProxy object for my entire process becomes unusable if I go offline, make an unsuccessful request, and then return online. It is important to make a request that does not work, otherwise there is no problem. Here is an example of how I can get the handle of this fragile proxy object.

var defaultHandler = new HttpClientHandler(); var fragileProxy = defaultHandler.Proxy; var httpClient = new HttpClient(defaultHandler); 

After some experimentation, I found that I could get a working proxy by calling System.Net.WebProxy.GetDefaultProxy ();

Then I implemented NetworkChangAwareProxy: IWebProxy. This is the correct proxy for my IWebProxy. Inside, it just goes and gets a new WebProxy.GetDefaultProxy () whenever NetworkChange.NetworkAddressChanged.

I plug it in when the application starts and the problem disappears.

 WebRequest.DefaultWebProxy = new NetworkChangeAwareProxy(); 

Hopefully someone will tell me that there is a better way to solve this problem. My specific question is about Store Store app apps. (metro applications)

System.Net.WebProxy.GetDefaultProxy () is unavailable, and System.Net.HttpWebRequest.DefaultWebProxy simply returns the same invalid proxy after going offline and returning.

How can I get a handle to a new IWebProxy object in a C # application for the Windows Store?

+7
source share
2 answers

System.Net.WebProxy.GetDefaultProxy () has been deprecated since at least .net 3, use System.Net.WebRequest.GetSystemWebProxy () instead. For WinRT, they removed many obsolete methods.

See http://msdn.microsoft.com/en-us/library/system.net.webproxy.getdefaultproxy.aspx and http://msdn.microsoft.com/en-us/library/system.net.webrequest. getsystemwebproxy.aspx

+2
source

You can use the web request to get the proxy:

 var req = WebRequest.Create(@"api/stat/stats/"); req.Proxy = WebRequest.GetSystemWebProxy(); req.Timeout = 10000; req.Method = "GET"; 
0
source

All Articles