【问题标题】:C# WebClient.DownloadString to access US site of a URLC# WebClient.DownloadString 访问美国站点的 URL
【发布时间】: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 标头。

标签: c# html webclient


【解决方案1】:

如果服务器以页面的本地化版本响应,您的选择是:

  1. 尝试在 URL 中包含您想要的版本,例如 http://someurl.com/en-us
  2. 访问网站,看看是否有设置语言的选项,如果有,请尝试在您的程序中进行设置

  3. 尝试使用代理、VPN、Tor 等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 2018-07-13
    相关资源
    最近更新 更多