【发布时间】:2015-06-15 10:38:11
【问题描述】:
我将 URL 传递到 WebClient.DownloadString("http://someurl.com") 以下载页面的 HTML,但它总是下载我所在国家/地区的页面版本(即http://someurl.com/en-cn)我需要下载该 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);
}
我是否缺少参数?我需要传递什么才能让它始终下载美国版页面?
【问题讨论】:
-
“我需要传递什么才能让它始终下载美国版页面?” - 这完全取决于网站如何确定要提供的语言。使用 Fiddler 检查流量,看看发生了什么。也许您需要一个
Accept-language标头。