Skip to main content
 首页 » 编程设计

c#之使用 System.Net.WebClient 读取页面内容时,有没有办法指定文化

2024年01月29日25lautakyan007

有没有办法使用 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 请求并读取/解析响应。发送此请求时,您可以使用 Headers 设置标题,例如 Accept-Language属性:

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

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