如何在 ASP.NET 应用程序中使用 Internet Explorer 中使用的代理
在我的 ASP.NET 应用程序中,我使用 WebRequest 类,并且我想使用默认的系统代理。这是我正在使用的代码。
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 ;
}
当作为控制台应用程序的一部分运行时,它工作得非常好 - 它正确地选择系统代理(我假设 PC 上登录用户的 IE 代理),但是,当作为 ASP 的一部分运行时,这不起作用.NET 应用程序位于同一台计算机上。没有找到代理。
我认为这是因为 ASP.NET 运行在系统注册表中没有 IE 代理设置的用户帐户下。我尝试在 web.config 文件中包含以下内容,但这不起作用。
<system.net>
<defaultProxy>
<proxy usesystemdefault="true" />
</defaultProxy>
</system.net>
我的问题是如何设置 ASP.NET 3.5 应用程序以正确使用 IE 使用的默认代理?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有系统范围的默认代理。因此您只能使用当前用户的配置。如果没有这样的配置,就会有问题。
您可以尝试在具有代理配置的用户下运行 ASP.NET 应用程序的应用程序池,或者查看组策略,据我所知,组策略确实支持设置系统范围的代理。
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.