How to use the proxy used in Internet Explorer in

2019-04-14 06:53发布

问题:

In my ASP.NET application I am using the WebRequest class and I want to use the default system proxy. Here is the code I am using.

private static bool CheckIfUriIsReachable(string uri)
{
    bool reachable = true;

    var request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = "HEAD";

    var proxy = WebRequest.GetSystemWebProxy();

    proxy.Credentials = CredentialCache.DefaultCredentials;
    request.Proxy = proxy;

    HttpWebResponse response = null;
    try
    {
        response = (HttpWebResponse)request.GetResponse();
    }
    catch (WebException)
    {
        reachable = false;
    }
    finally
    {
        if (response != null)
        {
            response.Close();
        }
    }

    return reachable ;
}

This works perfectly well when run as part of a console application - it correctly picks up the system proxy (I assume the IE proxy for the signed-on user on the PC), However, this does not work when run as part of an ASP.NET application on the same machine. No proxy is found.

I assume this is because ASP.NET is running under a user account that does not have an IE proxy setting in the system registry. I have tried to include the following in the web.config file but this does not work.

<system.net>
   <defaultProxy>
       <proxy usesystemdefault="true" />
   </defaultProxy>
</system.net>

My question is how to setup an ASP.NET 3.5 application to correctly use the default proxy used by IE?

回答1:

There is not system-wide default proxy. Thus you can only use th configuration of the current user. If there is no such configuration, you have a problem.

You can try to run the app pool of your ASP.NET application under a user with a proxy configuration or look into group policies, which, AFAIK, do support setting a system-wide proxy.