C#通过代理连接

Kal*_*lEl 91 c# proxy

我在办公室工作,需要通过特定的http代理进行所有连接.我需要编写一个简单的应用程序来从Web服务器查询一些值 - 如果没有代理,这很容易.如何使C#应用程序能够识别代理?如何通过代理进行任何类型的连接?

Cra*_*gTP 102

这可以通过编程,代码或声明性地在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();
Run Code Online (Sandbox Code Playgroud)

您基本上是将WebProxy对象分配给request对象的proxy属性.request然后,这将使用proxy您定义的.

要以声明方式实现相同的目的,您可以执行以下操作:

<system.net>
  <defaultProxy>
    <proxy
      proxyaddress="http://[your proxy address and port number]"
      bypassonlocal="false"
    />
  </defaultProxy>
</system.net>
Run Code Online (Sandbox Code Playgroud)

在您的web.config或app.config中.这将设置所有http请求将使用的默认代理.根据您需要实现的内容,您可能需要也可能不需要defaultProxy/proxy元素的某些附加属性,因此请参阅相关文档.

  • @Skuta - 我编辑了我的帖子以澄清这一点,并确保程序化和声明性示例实际上做同样的事情! (2认同)

Ode*_*ded 22

如果您使用WebClient,它有一个您可以使用的代理属性.

正如其他人所提到的,有几种方法可以自动执行代理设置检测/使用

Web.Config中:

<system.net>
   <defaultProxy enabled="true" useDefaultCredentials="true">
     <proxy usesystemdefault="true" bypassonlocal="true" />
   </defaultProxy>
</system.net>
Run Code Online (Sandbox Code Playgroud)

使用本文中描述的WebProxy类.


您也可以直接配置代理设置(配置或代码),然后您的应用将使用这些设置.

Web.Config中:

<system.net>
  <defaultProxy>
    <proxy
      proxyaddress="http://[proxy address]:[proxy port]"
      bypassonlocal="false"
    />
  </defaultProxy>
</system.net>
Run Code Online (Sandbox Code Playgroud)

码:

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();
Run Code Online (Sandbox Code Playgroud)


Rob*_*tie 6

如果您希望应用程序使用系统默认代理,请将其添加到Application.exe.config(其中application.exe是您的应用程序的名称):

<system.net>
   <defaultProxy enabled="true" useDefaultCredentials="true">
   <proxy usesystemdefault="true" bypassonlocal="true" />
   </defaultProxy>
</system.net>
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅System.Net上的MSDN文章


小智 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • "WebProxy.GetDefaultProxy"已经过时,因为Framework 4.5和此方法返回null.在使用`CorrentialCache.DefaultNetworkCredentials`之前,请更好地思考.如果你在CredentialCache中放了一些东西而你的代理需要这样的凭证,那么它应该可行.否则它将无济于事. (3认同)

Coe*_*aar 6

这个单线程对我有用:

CredentialCache.DefaultNetWorkCredentials

WebRequest.DefaultWebProxy.Credentials 是Internet Explorer中设置的代理设置.

CredentialCache.DefaultNetWorkCredentials 用于应用程序中的所有Internet连接.