C# 通过代理连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1938990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Connecting Through Proxy
提问by KalEl
I work in a office which requires all connections to be made through a specific http proxy. I need to write a simple application to query some values from a webserver - it's easy if there were no proxy. How can I make the C# application proxy-aware? How can I make any sort of connection through a proxy?
我在办公室工作,需要通过特定的 http 代理建立所有连接。我需要编写一个简单的应用程序来从网络服务器查询一些值 - 如果没有代理,这很容易。如何使 C# 应用程序能够识别代理?如何通过代理建立任何类型的连接?
采纳答案by CraigTP
This is easily achieved either programmatically, in your code, or declaratively in either the web.config or the app.config.
这可以通过编程方式、在您的代码中或在 web.config 或 app.config 中以声明方式轻松实现。
You can programmatically create a proxy like so:
您可以像这样以编程方式创建代理:
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();
You're basically assigning the WebProxy
object to the request
object's proxy
property. This request
will then use the proxy
you define.
您基本上是将WebProxy
对象分配给request
对象的proxy
属性。这request
将使用proxy
您定义的。
To achieve the same thing declaratively, you can do the following:
要以声明方式实现相同的目的,您可以执行以下操作:
<system.net>
<defaultProxy>
<proxy
proxyaddress="http://[your proxy address and port number]"
bypassonlocal="false"
/>
</defaultProxy>
</system.net>
within your web.config or app.config. This sets a default proxy that all http requests will use. Depending upon exactly what you need to achieve, you may or may not require some of the additional attributes of the defaultProxy/ proxyelement, so please refer to the documentation for those.
在您的 web.config 或 app.config 中。这将设置所有 http 请求都将使用的默认代理。根据您需要实现的具体目标,您可能需要也可能不需要defaultProxy/ proxy元素的一些附加属性,因此请参阅相关文档。
回答by Oded
If you are using WebClient
, it has a Proxyproperty you can use.
如果您正在使用WebClient
,它具有您可以使用的Proxy属性。
As other have mentioned, there are several ways to automate proxy setting detection/usage
正如其他人提到的,有几种方法可以自动检测/使用代理设置
Web.Config:
网络配置:
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="true" bypassonlocal="true" />
</defaultProxy>
</system.net>
Use of the WebProxy class as described in this article.
如本文所述使用 WebProxy 类。
You can also cofigure the proxy settings directly (config or code) and your app will then use those.
您还可以直接配置代理设置(配置或代码),然后您的应用程序将使用这些设置。
Web.Config:
网络配置:
<system.net>
<defaultProxy>
<proxy
proxyaddress="http://[proxy address]:[proxy port]"
bypassonlocal="false"
/>
</defaultProxy>
</system.net>
Code:
代码:
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();
回答by Robert Christie
If you want the app to use the system default proxy, add this to your Application.exe.config (where application.exe is the name of your application):
如果您希望应用程序使用系统默认代理,请将其添加到您的 Application.exe.config(其中 application.exe 是您的应用程序的名称):
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="true" bypassonlocal="true" />
</defaultProxy>
</system.net>
More details can be found on in the MSDN article on System.Net
可以在 System.Net上的MSDN 文章中找到更多详细信息
回答by Elijah Glover
Automatic proxy detection is a process by which a Web proxy server is identified by the system and used to send requests on behalf of the client. This feature is also known as Web Proxy Auto-Discovery (WPAD). When automatic proxy detection is enabled, the system attempts to locate a proxy configuration script that is responsible for returning the set of proxies that can be used for the request.
自动代理检测是系统识别 Web 代理服务器并用于代表客户端发送请求的过程。此功能也称为 Web 代理自动发现 (WPAD)。启用自动代理检测后,系统会尝试定位代理配置脚本,该脚本负责返回可用于请求的代理集。
回答by Foole
This code has worked for me:
这段代码对我有用:
WebClient wc = new WebClient();
wc.Proxy.Credentials = CredentialCache.DefaultCredentials;
回答by Seamus
Try this code. Call it before making any http requests. The code will use the proxy from your Internet Explorer Settings - one thing though, I use proxy.Credentials = ....
because my proxy server is an NTLM authenticated Internet Acceleration Server. Give it a whizz.
试试这个代码。在发出任何 http 请求之前调用它。该代码将使用您的 Internet Explorer 设置中的代理 - 不过,我使用proxy.Credentials = ....
这一点是因为我的代理服务器是经过 NTLM 身份验证的 Internet 加速服务器。给它一个吹口哨。
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);
}
}
回答by JCH2k
Foole's code worked perfectly for me, but in .NET 4.0, don't forget to check if Proxy is NULL, which means no proxy configured (outside corporate environment)
Foole 的代码非常适合我,但是在 .NET 4.0 中,不要忘记检查 Proxy 是否为 NULL,这意味着没有配置代理(在公司环境之外)
So here's the code that solved my problem with our corporate proxy
所以这是解决我的公司代理问题的代码
WebClient web = new WebClient();
if (web.Proxy != null)
web.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
回答by JD-V
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);
回答by Mukus
I am going to use an example to add to the answers above.
我将用一个例子来补充上面的答案。
I ran into proxy issues while trying to install packages via Web Platform Installer
我在尝试通过Web 平台安装程序安装软件包时遇到了代理问题
That too uses a config file which is WebPlatformInstaller.exe.config
这也使用了一个配置文件,即WebPlatformInstaller.exe.config
I tried the edits suggest in this IIS forumwhich is
我尝试了这个 IIS 论坛中的编辑建议,它是
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy enabled="True" useDefaultCredentials="True"/>
</system.net>
</configuration>
and
和
<?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>
None of these worked.
这些都没有奏效。
What worked for me was this -
对我有用的是这个 -
<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>
The module needed to be registered with Web Platform Installer in order to use it.
该模块需要在 Web 平台安装程序中注册才能使用。
回答by Coert Grobbelaar
This one-liner works for me:
这个单线对我有用:
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
CredentialCache.DefaultNetWorkCredentials
is the proxy settings set in Internet Explorer.
CredentialCache.DefaultNetWorkCredentials
是在 Internet Explorer 中设置的代理设置。
WebRequest.DefaultWebProxy.Credentials
is used for all internet connectivity in the application.
WebRequest.DefaultWebProxy.Credentials
用于应用程序中的所有互联网连接。