2015-06-15 21 views
1

我傳遞一個URL到WebClient.DownloadString(「http://someurl.com」)下載頁面的HTML,但它總是下載我國的頁面版本(即http://someurl.com/en-cn)我需要下載美國網站的網址。C#WebClient.DownloadString訪問一個URL的美國網站

這裏是我的功能,我稱之爲下載HTML:

public static String GetHtmlStringWC(string url) 
    { 
     string htmlString = string.Empty; 

     try 
     { 
      using (WebClient webClient = new WebClient()) 
      { 
       try 
       { 
        webClient.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"; 
        WebProxy myProxy = new WebProxy(); 
        myProxy.IsBypassed(new Uri(url)); 
        webClient.Proxy = myProxy; 
        htmlString = webClient.DownloadString(url); 
       } 
       catch (Exception ex) 
       { 
        throw; 
       } 
       finally 
       { 
        webClient.Dispose(); 
       } 
      } 
     } 
     catch (WebException wex) 
     { 
      throw; 
     } 
     catch (Exception ex) 
     { 
      throw; 
     } 
     finally { } 

     return htmlString.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty); 
    } 

我缺少一個參數?我需要通過什麼來始終下載美國版的頁面?

+0

_「我需要通過什麼才能讓它始終下載美國版本的頁面?」 - 這完全取決於網站如何確定要使用哪種語言。用Fiddler檢查交通情況,看看發生了什麼。也許你需要一個'Accept-language'頭文件。 – CodeCaster

回答

2

如果服務器與頁面的本地化版本響應你的選擇是:

  1. 嘗試包括要在URL像http://someurl.com/en-us
  2. 去的網站版本,並看看是否有一個選項來設置語言,如果你試圖在你的程序中嘗試這樣做

  3. 嘗試使用代理,VPN,Tor等。