WebProxy on Portable Class Library

2019-06-07 15:48发布

问题:

I'm building an Portable Class Library project. Using HttpClient class ( Installed from NuGet Packages ).

Now. I want to make HttpClient using Proxy by pass an HttpClientHandler to it constructor ( HttpClientHandler have an Proxy attribute, we will assign an WebProxy instance to it ). The problem is Portable Class Library not support WebProxy class. It have only IWebProxy interace.

I searched on Google, NuGet Package, but I can't find out any solution for this case. Please tell me. How can I solve this ( or another way to make HttpClient using Proxy )

回答1:

There is no implementation of IWebProxy for PCLs, but it is a very simple interface that you can easily implement on your own. Something like this for the same proxy on all destinations:

public class Proxy : System.Net.IWebProxy
{
    public System.Net.ICredentials Credentials
    {
        get;
        set;
    }

    private readonly Uri _proxyUri;

    public Proxy(Uri proxyUri)
    {
        _proxyUri = proxyUri;
    }

    public Uri GetProxy(Uri destination)
    {
        return _proxyUri;
    }

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