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

标签 c# .net

有没有办法使用 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方法将以字节数组的形式返回响应,您可以在将此数组转换为字符串时应用正确的编码。

关于c# - 使用 System.Net.WebClient 读取页面内容时,有没有办法指定文化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2722771/

相关文章:

c# - 如何检查 Ssh.NET 连接是否建立成功?

c# - 从用 C++ 编写的桌面应用程序过渡到基于 Web 的应用程序

.net - RxJS/Rx.Net 可观察订阅与事件 - 性能/线程

c# - CancellationTokens 可以跨 AppDomains 使用吗

c# - ORA-12705 : Cannot access NLS data files or invalid environment specified

c# - 为什么我的 Resharper NUnit session 停止工作,但在 Debug模式下工作

.net - 从 UserControl 的子控件传递事件 - 最直接的方法?

C#:有没有办法使用 lambda 表达式设置集合中对象的属性?

c# - .NET 数组 - "Length"、 "Count()"和 "Rank"之间的区别

c# - 在 Google Drive API 中设置文件夹权限