获取IE与DefaultWebProxy默认代理 [英] Get IE's default proxy with DefaultWebProxy
问题描述
我已经通过几乎所有的文档阅读我能找到,但我还没有找到使用 DefaultWebProxy()如何让IE的默认代理设置$ C $一个简单的工作示例C>。
I've read through pretty much all the documentation I can find but I'm yet to find a simple working example of how to get IE's default proxy settings using DefaultWebProxy()
.
这代码似乎编译和工作,但我怎么然后继续前进,获得代理URI作为一个字符串?
This code seems to compile and work but how do I then go ahead and get the proxy URI as a string?
HttpWebRequest webRequest =
(HttpWebRequest)HttpWebRequest.Create("http://www.google.com");
if (WebRequest.DefaultWebProxy != null)
{
webRequest.Proxy = WebRequest.DefaultWebProxy;
}
编辑:
由于提交这个问题,我发现,一个或多个代理服务器可以为不同的目的地进行设置或绕过(也许是本地Intranet目的地)。这就是为什么你需要指定一个URI来 GetProxy()
。它需要知道让代理为其目的地。如果自动检测设置,在Internet选项设置,浏览器会寻找你的本地域中的PAC文件。 PAC文件包含一个JavaScript函数,它确定代理地址为给定目标。
Since submitting this question I have found that one or many proxies can be set for different destinations or bypassed (perhaps for local intranet destinations). That's why you need to specify a URI to GetProxy()
. It needs to know which destination to get the proxy for. If "Automatically Detect Settings" is set in Internet Options the browser will look for a PAC file on your local domain. The PAC file contains a Javascript function which determines the proxy address for a given destination.
推荐答案
WebRequest.DefaultWebProxy
实现的 IWebProxy
接口。您可以使用 GetProxy
方法来获取代理的URI:
WebRequest.DefaultWebProxy
implements the IWebProxy
interface. You can use the GetProxy
method to get the proxy's URI:
var uri = WebRequest.DefaultWebProxy.GetProxy(new Uri("http://www.google.com"));
回应评论:
Response to a comment:
您需要通过URI来 GetProxy
,因为这是微软如何实现的吧...
You need to pass the uri to GetProxy
because that's how Microsoft implemented it...
说真的,我相信这是所以,因为你可以配置浏览器绕过一些地址的代理服务器。如果您传递的这一个绕过地址,你可能会得到不同的结果。
Seriously, I believe this is so because you can configure the browser to bypass the proxy for some addresses. If you pass one of this bypassed addresses, you will probably get a different result.
这篇关于获取IE与DefaultWebProxy默认代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!