WebProxy on Portable Class Library

2019-06-07 14:57发布

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条回答
Rolldiameter
2楼-- · 2019-06-07 15:34

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;
    }
}
查看更多
登录 后发表回答