[英]C# Connecting Through Proxy
我在辦公室工作,要求所有連接都必須通過特定的http代理進行。 我需要編寫一個簡單的應用程序來從Web服務器查詢一些值-如果沒有代理,這很容易。 如何使C#應用程序可感知代理? 如何通過代理建立任何形式的連接?
這很容易以編程方式在您的代碼中或以聲明性方式在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();
如果您希望應用程序使用系統默認代理,請將其添加到Application.exe.config(其中application.exe是應用程序的名稱):
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy usesystemdefault="true" bypassonlocal="true" />
</defaultProxy>
</system.net>
在System.Net上的MSDN文章中可以找到更多詳細信息。
這種單線適用於我:
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
CredentialCache.DefaultNetWorkCredentials
是Internet Explorer中設置的代理設置。
WebRequest.DefaultWebProxy.Credentials
用於應用程序中的所有Internet連接。
試試這個代碼。 在發出任何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);
}
}
Foole的代碼對我來說非常完美,但是在.NET 4.0中,請不要忘記檢查Proxy是否為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;
自動代理檢測是系統識別Web代理服務器並用來代表客戶端發送請求的過程。 此功能也稱為Web代理自動發現(WPAD)。 啟用自動代理檢測后,系統會嘗試找到一個代理配置腳本,該腳本負責返回可用於該請求的代理集。
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平台安裝程序安裝軟件包時遇到代理問題
這也使用了一個配置文件,即WebPlatformInstaller.exe.config
我在這個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 Platform Installer中注冊才能使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.