2012-11-26 18 views
0

我有以下情況:的WebRequest與代理和證書

要連接到我使用代理上網......我不知道到底是代理設置,但我們使用URL自動檢索配置等...

之後,要連接到外部資源,我必須提供與我的機器憑證不同的用戶憑證。

我現在的問題:

我該如何連接一些資源,例如谷歌? 我有以下代碼ofcourse不起作用:

 string url = @"http://www.google.com"; 
     WebRequest request = WebRequest.Create(url); 

     Console.WriteLine("Starting"); 

     using (WebResponse webResponse = request.GetResponse()) 
     { 
      //TODO 
     } 
     Console.WriteLine("Finished"); 
     Console.ReadLine(); 

也試圖與這個額外的道具:

 request.Proxy = WebRequest.DefaultWebProxy; 
     request.Credentials = CredentialCache.DefaultCredentials; 
     request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; 
     NetworkCredential networkCredential = new NetworkCredential("usr", "psw"); 
     request.Credentials = networkCredential; 

任何想法,該怎麼辦呢?

回答

1

你可以嘗試將類似於您的App.Config中的東西:

<system.net> 
    <defaultProxy enabled="true" useDefaultCredentials="true"> 
    </defaultProxy> 
</system.net> 

如果不工作,你可以建立自己的代理類是這樣的:

添加類似於您的應用程序的東西。配置:

<system.net> 
    <defaultProxy enabled="true" useDefaultCredentials="false"> 
    <module type="Your.MyProxy, YourApp" /> 
    </defaultProxy> 
</system.net> 

其中Your.Proxy是您的代理類的名稱空間和類名稱。然後創建一個類似的類:

// In namespace Your 
// ... 

public class MyProxy: IWebProxy 
{ 
    /// ==================================================================== 
    /// <summary> 
    /// The credentials to submit to the proxy server for authentication. 
    /// </summary> 
    /// <returns>An <see cref="T:System.Net.ICredentials"/> instance that contains the 
    /// credentials that are needed to authenticate a request to the proxy server.</returns> 
    /// ==================================================================== 
    public ICredentials Credentials 
    { 
     get 
     { 
      // Read all values from the AppSettings 
      string username = ConfigurationManager.AppSettings["ProxyUsername"].ToString(); 
      string password = ConfigurationManager.AppSettings["ProxyPassword"].ToString(); 
      string domain = ConfigurationManager.AppSettings["ProxyDomain"].ToString(); 
      return new NetworkCredential(username, password, domain); 
     }    
     set { } 
    } 

    /// ==================================================================== 
    /// <summary> 
    /// Returns the URI of a proxy. 
    /// </summary> 
    /// <param name="destination">A <see cref="T:System.Uri"/> that specifies the requested 
    /// Internet resource.</param> 
    /// <returns> 
    /// A <see cref="T:System.Uri"/> instance that contains the URI of the proxy used to 
    /// contact <paramref name="destination"/>. 
    /// </returns> 
    /// ==================================================================== 
    public Uri GetProxy(Uri destination) 
    { 
     // Use the proxy server specified in AppSettings 
     string proxy = ConfigurationManager.AppSettings["ProxyServer"].ToString(); 
     return new Uri(proxy); 
    } 

    /// ==================================================================== 
    /// <summary> 
    /// Indicates that the proxy should not be used for the specified host. 
    /// </summary> 
    /// <param name="host">The <see cref="T:System.Uri"/> of the host to check for proxy use.</param> 
    /// <returns> 
    /// true if the proxy server should not be used for <paramref name="host"/>; otherwise, false. 
    /// </returns> 
    /// ==================================================================== 
    public bool IsBypassed(Uri host) 
    { 
     // Ignore localhost URIs 
     string[] bypassUris = ConfigurationManager.AppSettings["ProxyBypass"].ToString().Split(','); 
     foreach (string bypassUri in bypassUris) 
     { 
      if (host.AbsoluteUri.ToLower().Contains(bypassUri.Trim().ToLower())) 
      { 
       return true; 
      } 
     } 

     return false; 
    } 
} 

然後,你可以添加一些更多的設置,以您的App.Config中,如:

<!-- New Proxy settings --> 
<add key="ProxyUsername" value="User123" /> 
<add key="ProxyPassword" value="Password456" /> 
<add key="ProxyDomain" value="your.domain" /> 
<add key="ProxyServer" value="http://123.456.789.000:8080" /> 
<add key="ProxyBypass" value="localhost, another_server" /> 

我希望能幫助您在正確的軌道上?

+0

所以你不能只是定義一些東西,你真的需要創建這個類?還有很多這些屬性我不知道,當我使用瀏覽器衝浪時,它會根據設置中提供的鏈接設置代理。有另一種方法嗎? – Alnedru

+0

你不一定需要這門課。最初你應該嘗試使用默認憑證的第一個選項。然後我進一步擴展,以顯示如果您需要更多控制(如果使用默認憑證並不總是有效),您可以如何繼續使用它。 – Belogix