Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...