Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF中的C#html代码读取器错误_C#_Wpf_Xaml_Encoding_Httpwebrequest - Fatal编程技术网

WPF中的C#html代码读取器错误

WPF中的C#html代码读取器错误,c#,wpf,xaml,encoding,httpwebrequest,C#,Wpf,Xaml,Encoding,Httpwebrequest,我在尝试从中获取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"

我在尝试从中获取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>

}

我认为这是因为网站使用了压缩,而.NET没有正确地预期/处理它。这可能是因为通常这只通过HTTPS完成。尝试通过https。。。即

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

另外,请确保使用正确的编码

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