代理与HTTP请求 [英] Proxy with HTTP Requests

查看:137
本文介绍了代理与HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可能路由通过代理GET请求通过指定主机作为代理?或者你必须设置数据包的目的地是哪里?

Would it be possible to route a GET request through a proxy by specifying the host as the proxy? Or would you have to set the destination of the packet?

我想生成HTTPRequestMessage并将其路由通过代理。但是,我没有设置请求的目的地的精细程度控制发送出去。

I am trying to generate an HTTPRequestMessage and route it through a proxy. However, I do not have fine level control of setting the destination of the request being sent out.

推荐答案

我是能够增加一个代理HttpClient的,HttpWebRequest和HttpRequestMessage。他们没有被一起使用,但我只是发现HTTP请求与代理两种方式。要在Windows商店/城域应用做到这一点,你就必须实现IWebProxy。

I was able to add a proxy to HttpClient, HttpWebRequest and HttpRequestMessage. They do not have to be used together, but I just found two ways of making HTTP Requests with proxy. To do this in windows store/metro applications, you would have to implement IWebProxy.

看看这个实施IWebProxy:的 http://social.msdn.microsoft.com /论坛/ windowsapps / EN-US / 6e20c2c0-105c-4d66-8535-3ddb9a048b69 /错误缺失型webproxy-着集代理,当时在那里 - 是最AppConfig的

Take a look at this for implementing IWebProxy: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/6e20c2c0-105c-4d66-8535-3ddb9a048b69/bug-missing-type-webproxy-cant-set-proxy-then-where-is-the-appconfig

然后,所有你需要做的是设置的HttpClient或HttpWebRequest的代理:

Then all you need to do is set the proxy for HttpClient or HttpWebRequest:

HttpClient的:

HttpClient:

HttpClientHandler aHandler = new HttpClientHandler();
IWebProxy proxy = new MyProxy(new Uri("http://xx.xx.xx.xxx:xxxx"));
proxy.Credentials = new NetworkCredential("xxxx", "xxxx");
aHandler.Proxy = proxy;
HttpClient client = new HttpClient(aHandler);



HttpWebRequest的:

HttpWebRequest:

HttpWebRequest webrequest = (HttpWebRequest)WebRequest.CreateHttp(uri);
IWebProxy proxy = new MyProxy(new Uri("http://xx.xx.xx.xxx:xxxx"));
proxy.Credentials = new NetworkCredential("xxxx", "xxxx");
webrequest.Proxy = proxy;



HttpRequestMessage

HttpRequestMessage

一旦你构建一个HttpRequestMessage ,你可以使用上述方法(HttpClient的)发送该请求报文将通过没有任何额外的工作代理进行路由。

Once you construct an HttpRequestMessage, you can use the method above (HttpClient) to send this request message and it will be routed through the proxy without any additional work.

这篇关于代理与HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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