How to get the value when C# crawler encounters EventStream data

Statement: This article is for study and research only. It is prohibited to be used for illegal purposes, otherwise you will be responsible for the consequences. If there is any infringement, please inform us to delete it. Thank you!

Today when I called the interface of a website, I found that the data format was like this. It was the first time I encountered it.

 The normal thing should be like this, there is a "response", and then the response contains some returned data.

And this is very strange, there is no "response", only an EventStream. I've been searching online for a long time but haven't seen any relevant tutorials on how to obtain this data. There are some, but you need to be a member. I'm really convinced.

Just put the code:

            //url和postdata是接口地址和post参数
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36";
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postdata);
            request.ContentLength = byteArray.Length;

            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string line = string.Empty;
                    while (!reader.EndOfStream)
                    {
                        line += reader.ReadLine();
                    }
                    return line;//这个就是EventStream里面所有的数据
                }
            }

In fact, it’s thanks to chatgpt!

Guess you like

Origin blog.csdn.net/qq_51502150/article/details/129364128