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
522 views
in Technique[技术] by (71.8m points)

c# - Using Proxy Automatic Configuration from IE Settings in .Net

I'm having trouble getting Proxy Automatic Configuration (PAC) in IE options to work as expected using .Net WebRequest.

According to this article:
Proxy Detection Take the Burden Off Users with Automatic Configuration in .NET

The system proxy should be set by default with to each WebRequest.

That's how the proxy.js pac file looks like:

function FindProxyForURL(url, host)
{
  return "PROXY ProxyServerName:3118; DIRECT;";
}

I also took a look at this post: How should I set the default proxy to use default credentials?

Which suggests to add this in the app.config:

<system.net>
  <defaultProxy useDefaultCredentials="true" />
</system.net>

Adding this did not help.

I created a small console application just to test this out.. here it is:

static void Main(string[] args)
{
    HttpWebRequest request = null;
    try
    {               
        String resolvedAddress = WebRequest.DefaultWebProxy.GetProxy(new Uri("http://www.google.com")).ToString();
        Console.WriteLine("Proxy for address is: " + resolvedAddress);

        Uri m_URLToTest = new Uri("http://www.google.com");
        request = WebRequest.Create(m_URLToTest) as HttpWebRequest;
        request.Method = "GET";
        request.KeepAlive = false;
        request.Timeout = 5000;
        request.Proxy = WebRequest.DefaultWebProxy;
        WebResponse response = request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string message = reader.ReadToEnd();
    }
    catch (Exception ex)
    {
        Console.Write("Exception");
    }

}

The output: Proxy for address is http://www.google.com

instead of Proxy for address is ProxyServerName:3118

It happens only when using auto configuration script...

Did I miss anything? Please help!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Found the solution!

It is really important that the mime type of the PAC file would be: [Content-type: application/x-ns-proxy-autoconfig]

Other mime types might not work.

Make sure using fiddler2 (with cache disabled) that the mime type is appropriate. Some configurations might show Content-Type: text/plain which is bad.


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

...