Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

c# - Get IE's default proxy with DefaultWebProxy

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().

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

EDIT:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...