繁体   English   中英

C# 使用 HttpWebRequest 不起作用

[英]C# using HttpWebRequest doesn't work

在打开时,使用HttpWebRequest 为什么要访问

https://web4.sa8888.net/sport/Games.aspx?lang=3&device=pc

拿不到里面的数据?

string strResult = "";
try
{
    string url = "https://web4.sa8888.net/sport/Games.aspx?lang=3&device=pc";

    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.AutomaticDecompression = DecompressionMethods.GZip;

    request.Method = "GET";
    request.Timeout = 30000;
    request.KeepAlive = true; 
    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
    request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
    request.Headers.Set("Pragma", "no-cache");
    //request.ContentLength = bs.Length;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream streamReceive = response.GetResponseStream();
    Encoding encoding = Encoding.GetEncoding("UTF-8");
    StreamReader streamReader = new StreamReader(streamReceive, encoding);
    strResult = streamReader.ReadToEnd();
}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}
textBox1.Text=strResult;

您链接到的页面是使用 javascript 构建的,因此您所看到的并不是源代码的一部分。 例如,如果您查看https://web4.sa8888.net/js/v1000/sport/games/IceHockey.js,您会看到他们在那里构建了大量表格数据。

HttpWebRequest 只会为你获取一个文件,它不会像真正的浏览器那样运行 javascript 等。 这就是为什么你没有得到你想要的数据。

在查看更多之后,似乎他们通过 websockets 以二进制格式流式传输信息,所以这是一个非常重要的设置来获取数据(并且可能至少部分地构建以避免人们抓取他们的数据)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM