【问题标题】:Is there a way to specify the culture when using System.Net.WebClient to read the contents of a page?有没有办法在使用 System.Net.WebClient 读取页面内容时指定文化?
【发布时间】:2010-04-27 15:55:29
【问题描述】:

有没有办法使用 System.Net.WebClient 呈现具有特定文化的页面?

例如:

System.Net.WebClient client = new System.Net.WebClient();

CultureInfo myCulture = System.Globalization.CultureInfo.GetCultureInfo("es-ES");
// Do something to specify the culture info

client.DownloadString(someUrl);

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    WebClient 类所做的只是执行 HTTP 请求并读取/解析响应。发送此请求时,您可以使用Headers 属性设置诸如Accept-Language 之类的标头:

    client.Headers["Accept-Language"] = "es-ES";
    

    如果您在谈论编码,当您使用DownloadString 方法时,它将查找响应标头以使用正确的编码,如果服务器发送错误的响应标头,您可能会遇到编码问题。如果是这种情况,您可以使用DownloadData 方法,该方法会将响应作为字节数组返回给您,并且您可以在将此数组转换为字符串时应用正确的编码。

    【讨论】:

    • WebClient wc = new WebClient(); wc.Headers.Add("接受语言", "en-uk,uk;q=0.8,en-us;q=0.5,en;q=0.3");为我工作
    猜你喜欢
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多