使用System.Net.WebClient读取页面内容时,是否可以指定文化? [英] Is there a way to specify the culture when using System.Net.WebClient to read the contents of a page?

查看:42
本文介绍了使用System.Net.WebClient读取页面内容时,是否可以指定文化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用System.Net.WebClient呈现具有特定区域性的页面?

Is there a way to render a page with a specific culture using 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);

推荐答案

WebClient类所做的全部工作是执行HTTP请求并读取/解析响应.发送此请求时,您可以使用

All that the WebClient class does is to perform an HTTP request and read/parse the response. When sending this request you could set headers such as Accept-Language using the Headers property:

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

如果您在谈论编码,则使用 DownloadString 方法,它将查找响应标头以使用正确的编码,如果服务器发送错误的响应标头,则可能存在编码问题.在这种情况下,您可以使用 DownloadData 方法,该方法将以字节数组的形式返回响应,并且在将该数组转换为字符串时可以应用正确的编码.

If you are talking about the encoding, when you use the DownloadString method it will look for the response headers in order to use the proper encoding and if the server sends wrong response header you might have encoding problems. If this is the case you could use the DownloadData method which will return you the response as a byte array and you could apply the proper encoding when converting this array to string.

这篇关于使用System.Net.WebClient读取页面内容时,是否可以指定文化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆