使用 DefaultWebProxy 获取 IE 的默认代理 [英] Get IE's default proxy with DefaultWebProxy
问题描述
我已经阅读了几乎所有我能找到的文档,但我还没有找到一个简单的工作示例,说明如何使用 DefaultWebProxy()
获取 IE 的默认代理设置.
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;
}
自从提交这个问题后,我发现可以为不同的目的地设置一个或多个代理或绕过(可能是本地内联网目的地).这就是您需要为 GetProxy()
指定 URI 的原因.它需要知道获取代理的目的地.如果在 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"));
回复评论:
您需要将 uri 传递给 GetProxy
,因为 Microsoft 就是这样实现的...
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.
这篇关于使用 DefaultWebProxy 获取 IE 的默认代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!