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?