c# - 为什么它在消息框上显示问号而不是文本

标签 c# .net httpwebrequest

为什么在消息框上显示问号而不是文本

enter image description here

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://teamxor.net/vb/tx48/"+ page);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

StreamReader sr = new StreamReader(response.GetResponseStream());

string result = sr.ReadToEnd();

Regex r = new Regex("<div>.*?</div>");
MatchCollection mr = r.Matches(result);

foreach (Match m in mr)
{
    MessageBox.Show(m.Value, "Test", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
}      

最佳答案

问题在于使用了非默认代码页。您的 HTML 显示您正在使用代码页 1256。您必须告诉 .NET,否则它认为它是 UTF-8:

StreamReader sr = new StreamReader( response.GetResponseStream()
                                  , Encoding.GetEncoding(1256) // <-- this one
                                  );

使用Encoding.GetEncoding获得正确的代码页。我建议改用 UTF8,因为它很容易被 .NET 识别。

关于c# - 为什么它在消息框上显示问号而不是文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27633606/

相关文章:

c# - 为什么允许从函数内返回不安全的指针?

c# - 为什么 ObservableCollection 隐藏 INotifyPropertyChanged.PropertyChanged

.net - SQL Express 作为主数据库

.net - 在 httpwebrequest 期间,什么可能导致 .docx 文件损坏?

c# - 为什么这个 Assert.Throws 调用会以这种方式解析?

c# - 如何从代码启动 Windows Azure 存储模拟器 V3.0

c# - Windows 窗体 WebBrowser 控件和 iframe

c# - 使用 List<int> 作为参数模拟一个方法,并使用 Moq 返回 List<>

c# - 使用C#将数据HttpPost到网页

javascript - 通过 POST 请求将 jsondata 附加到在 Extjs 中加载 jsonstore 的调用