你可以通过编写代码或在 web.config 或 app.config 中声明来轻松实现此目标。
以下是以编程方式创建代理的示例:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("[ultimate destination of your request]");
WebProxy myproxy = new WebProxy("[your proxy address]", [your proxy port number]);
myproxy.BypassProxyOnLocal = false;
request.Proxy = myproxy;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
你基本上是将WebProxy
对象分配给request
对象的proxy
属性。这个request
然后将使用你定义的proxy
。
为了以声明方式实现相同的内容,你可以执行以下操作:
<system.net>
<defaultProxy>
<proxy
proxyaddress="http://[your proxy address and port number]"
bypassonlocal="false"
/>
</defaultProxy>
</system.net>
你可以在你的web.config或app.config文件中设置一个默认代理,所有http请求都将使用该代理。根据你需要实现的功能,你可能需要一些额外属性的defaultProxy/proxy元素,请参考相关文档了解详情。
如果你正在使用WebClient
,它有一个你可以使用的Proxy属性。
正如其他人所提到的,有几种自动化代理设置检测/使用的方法。
Web.Config:
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="true" bypassonlocal="true" />
</defaultProxy>
</system.net>
使用 这篇文章中描述的 WebProxy 类。
你也可以直接配置代理设置(通过代码或者配置文件),这样你的应用程序就会使用这些设置。
Web.Config:
<system.net>
<defaultProxy>
<proxy
proxyaddress="http://[proxy address]:[proxy port]"
bypassonlocal="false"
/>
</defaultProxy>
</system.net>
代码:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");
WebProxy myproxy = new WebProxy("[proxy address]:[proxy port]", false);
request.Proxy = myproxy;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
CredentialCache.DefaultNetWorkCredentials
是Internet Explorer中设置的代理设置。
WebRequest.DefaultWebProxy.Credentials
用于应用程序中所有的网络连接。
如果你希望该应用程序使用系统默认代理,将以下内容添加到你的Application.exe.config文件中(其中application.exe是你应用程序的名称):
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="true" bypassonlocal="true" />
</defaultProxy>
</system.net>
更多详细信息可以在System.Net的MSDN文章中找到。
尝试使用以下代码,在进行任何http请求之前调用它。该代码将使用您互联网浏览器设置中的代理 - 有一件事情需要注意,我使用 proxy.Credentials = ....
因为我的代理服务器是经过NTLM身份验证的Internet Acceleration Server。尽情体验吧。
static void setProxy()
{
WebProxy proxy = (WebProxy)WebProxy.GetDefaultProxy();
if(proxy.Address != null)
{
proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
WebRequest.DefaultWebProxy = new System.Net.WebProxy(proxy.Address, proxy.BypassProxyOnLocal, proxy.BypassList, proxy.Credentials);
}
}
WebProxy.GetDefaultProxy
方法已过时并会返回 null。在使用CredentialCache.DefaultNetworkCredentials
之前,请仔细考虑。如果您已将某些凭据放入 CredentialCache 中且您的代理需要此类凭据,则应该能够正常工作。否则它无法帮助您。 - cassandradFoole的代码对我来说完美运行,但在.NET 4.0中,不要忘记检查代理是否为NULL,这意味着没有配置代理(公司环境之外)。
下面是解决我们公司代理问题的代码:
WebClient web = new WebClient();
if (web.Proxy != null)
web.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
这段代码对我非常有效:
WebClient wc = new WebClient();
wc.Proxy.Credentials = CredentialCache.DefaultCredentials;
var getHtmlWeb = new HtmlWeb() { AutoDetectEncoding = false, OverrideEncoding = Encoding.GetEncoding("iso-8859-2") };
WebProxy myproxy = new WebProxy("127.0.0.1:8888", false);
NetworkCredential cred = (NetworkCredential)CredentialCache.DefaultCredentials;
var document = getHtmlWeb.Load("URL", "GET", myproxy, cred);
我将使用一个例子来补充上面的答案。
在尝试通过Web Platform Installer安装软件包时,我遇到了代理问题。
这也使用了一个名为WebPlatformInstaller.exe.config的配置文件。
我尝试了this IIS论坛中建议的修改方法。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy enabled="True" useDefaultCredentials="True"/>
</system.net>
</configuration>
并且
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy>
<proxy
proxyaddress="http://yourproxy.company.com:80"
usesystemdefault="True"
autoDetect="False" />
</defaultProxy>
</system.net>
</configuration>
这些都没有起作用。
对我有用的是这个 -
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="false">
<module type="WebPI.Net.AuthenticatedProxy, WebPI.Net, Version=1.0.0.0, Culture=neutral, PublicKeyToken=79a8d77199cbf3bc" />
</defaultProxy>
</system.net>
需要在 Web 平台安装程序中注册该模块,才能使用它。
BypassProxyOnLocal
属性为True(如果需要的话)。 - CraigTP