2017-02-28 102 views
0

我试图从http://mp3.zing.vn(与其他网站没有问题), 我收到的代码this得到的HTML代码时有问题。请帮我解决这个问题。C#html代码阅读器错误WPF

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)); 
    } 
} 

}

回答

0

我想这是因为该网站使用压缩和.NET并不指望/处理它正确。这可能是因为通常只能通过HTTPS完成。尝试通过https ...即。 https://mp3.zing.vn

如果不解决您的问题,我建议你这样做: request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

此外,请务必使用正确的编码...

+0

非常感谢你! –