可移植类库上的WebProxy [英] WebProxy on Portable Class Library

查看:90
本文介绍了可移植类库上的WebProxy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个可移植类库项目.使用HttpClient类(从NuGet软件包安装).

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

现在.我想通过将HttpClientHandler传递给它的构造函数来使HttpClient使用Proxy(HttpClientHandler具有Proxy属性,我们将为其分配一个WebProxy实例). 问题是可移植类库不支持WebProxy类.它只有IWebProxy接口.

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.

我在Google上搜索了NuGet Package,但找不到针对这种情况的任何解决方案. 请告诉我.我该如何解决(或使用Proxy制作HttpClient的另一种方法)

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 )

推荐答案

没有针对PCL的IWebProxy实现,但这是一个非常简单的界面,您可以轻松地自行实现.对于所有目标上的同一代理,都是这样的:

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;
    }
}

这篇关于可移植类库上的WebProxy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆