[英]C# html code reader error in WPF
嘗試從http://mp3.zing.vn獲取html代碼時,我遇到了問題(其他網站沒有問題),我收到的代碼是這樣的 。 請幫我解決這個問題。
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
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));
}
}
}
我認為這是因為該網站使用壓縮功能,而.NET並不期望/正確處理它。 這可能是因為通常這僅通過HTTPS完成。 嘗試https ...即。 https://mp3.zing.vn
如果那不能解決您的問題,我建議您這樣做: request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
另外,請確保使用正確的編碼...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.