[英]C# html code reader error in WPF
I have a problem when trying to get html code from http://mp3.zing.vn (no problem with other sites), the code I received like this . 尝试从http://mp3.zing.vn获取html代码时,我遇到了问题(其他网站没有问题),我收到的代码是这样的 。 Please help me to solve this problem. 请帮我解决这个问题。
MainWindow.xaml MainWindow.xaml
<Window x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:test"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<TextBox Text="{Binding Html}"/>
</Grid>
</Window>
MainWindow.xaml.cs MainWindow.xaml.cs
namespace test{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
public class ViewModel : INotifyPropertyChanged
{
public string Html { get; set; }
public ViewModel()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://mp3.zing.vn");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
readStream = new StreamReader(receiveStream, Encoding.ASCII, true);
Html = readStream.ReadToEnd();
OnPropertyChanged("Html");
response.Close();
readStream.Close();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
} }
I think this is because the web site uses compression and .NET does not expect/handle it correctly. 我认为这是因为该网站使用压缩功能,而.NET并不期望/正确处理它。 This is probably because usually this is only done over HTTPS. 这可能是因为通常这仅通过HTTPS完成。 Try over https... ie. 尝试https ...即。 https://mp3.zing.vn https://mp3.zing.vn
If that does not solve your problem I suggest you do: request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
如果那不能解决您的问题,我建议您这样做: request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
Also, Make sure you use the right encoding... 另外,请确保使用正确的编码...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.