我在办公室工作,需要通过特定的http代理进行所有连接。我需要编写一个简单的应用程序来从Web服务器查询一些值 - 如果没有代理,这很容易。如何使C#应用程序能够识别代理?如何通过代理进行任何类型的连接?
答案 0 :(得分:97)
这可以通过编程,代码或声明性地在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元素的某些其他属性,因此请参阅相关文档。
答案 1 :(得分:21)
如果您使用的是WebClient
,则可以使用Proxy属性。
正如其他人所提到的,有几种方法可以自动执行代理设置检测/使用
Web.Config中:
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="true" bypassonlocal="true" />
</defaultProxy>
</system.net>
使用this article中描述的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();
答案 2 :(得分:6)
试试这段代码。在发出任何http请求之前调用它。代码将使用Internet Explorer设置中的代理 - 但有一点,我使用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);
}
}
答案 3 :(得分:6)
这个单行程适合我:
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
CredentialCache.DefaultNetWorkCredentials
是Internet Explorer中设置的代理设置。
WebRequest.DefaultWebProxy.Credentials
用于应用程序中的所有互联网连接。
答案 4 :(得分:5)
如果您希望应用程序使用系统默认代理,请将其添加到Application.exe.config(其中application.exe是您的应用程序的名称):
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="true" bypassonlocal="true" />
</defaultProxy>
</system.net>
中找到
答案 5 :(得分:4)
Foole的代码对我来说非常合适,但是在.NET 4.0中,不要忘记检查Proxy是否为NULL,这意味着没有配置代理(公司外部环境)
所以这是用我们的公司代理解决我的问题的代码
WebClient web = new WebClient();
if (web.Proxy != null)
web.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
答案 6 :(得分:3)
此代码对我有用:
WebClient wc = new WebClient();
wc.Proxy.Credentials = CredentialCache.DefaultCredentials;
答案 7 :(得分:0)
自动代理检测是一个过程,通过该过程识别Web代理服务器 系统并用于代表客户端发送请求。此功能也称为 Web代理自动发现(WPAD)。启用自动代理检测时,系统 尝试查找负责返回可用于请求的代理集的代理配置脚本。
答案 8 :(得分:0)
public function destroy($id)
{
$pv = PV::find($id);
$pv->delete();
return true;
}