Matti-Koopa
Matti-Koopa

Reputation: 278

Set a custom WebProxy for a single HTTP request in .NET Core / UWP

I need a simple HTTP GET request to work with a custom proxy. I'm not talking about getting the Windows default proxy settings here but custom ones.

In a Xamarin Android app, the following code does work:

string myProxy = "100.100.100.100:7777";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://my_uri"));
request.Proxy = new WebProxy(myProxy, false);`

In an Windows Universal App project, the class WebProxy isn't implemented. So I followed this answer to create a custom implementation: Location of IWebProxy implementation for .NET Core

My code is now:

public class MyProxy : IWebProxy
{
    public Uri GetProxy(Uri destination)
    {                
       return new Uri("http://100.100.100.100:7777");
    }

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

    public ICredentials Credentials { get; set; }
}

and then...

request.Proxy = new MyProxy();

It doesn't work though. In fact, if I set a breakpoint inside of GetProxy, it doesn't even get hit. It just ignores the Proxy setting and uses the Windows 10 default settings.

Upvotes: 2

Views: 2631

Answers (1)

Jay Zuo
Jay Zuo

Reputation: 15758

Although we can use HttpWebRequest Class in UWP apps, but if we look at the Version Information of HttpWebRequest.Proxy Property, we will find this property is also not available in UWP like WebProxy Class. So your code won't work.

In .NET Core/UWP, System.Net.HttpWebRequest class is in System.Net.Requests library.

The API surface for .NET Core 5 is the same as that available for Windows 8.1 apps and is very limited compared to the surface in the .NET Framework. This is intentional and we highly encourage switching to the HttpClient API instead – that is where our energy and innovation will be focused going forward.

This library is provided purely for backward compatibility and to unblock usage of .NET libraries that use these older APIs. For .NET Core, the implementation of HttpWebRequest is actually based on HttpClient (reversing the dependency order from .NET Framework). As mentioned above, the reason for this is to avoid usage of the managed .NET HTTP stack in a UWP app context and move towards HttpClient as a single HTTP client role API for .NET developers.

For more info, please see .NET Networking APIs for UWP Apps.

And for HttpClient:

For both APIs, proxy settings are automatically obtained from Internet Explorer/Microsoft Edge settings and are used for all the HTTP calls by default. This enables apps to automatically work even if the user is connected to the internet through a proxy. Neither API provides a way to specify a custom proxy for your app. However, you can choose to not use the default proxy by setting HttpClientHandler.UseProxy to false (for System.Net.Http) or HttpBaseProtocolFilter.UseProxy to false (for Windows.Web.Http).

For more info, please see Demystifying HttpClient APIs in the Universal Windows Platform.

So we can't use a custom proxy for a single HTTP request by now. But you are welcome to vote on UserVoice to ask for this feature.

Upvotes: 3

Related Questions