从URL http请求流中获取字节,其中包括来自ajax调用的延迟数据

时间:2019-01-31 20:57:54

标签: c# http httpwebrequest httpwebresponse

我有一个网页,一旦加载,就会触发ajax调用以获取可能需要几秒钟的其他数据。我希望能够对网页网址执行GET请求,并在响应到达时访问响应流,包括ajax调用返回的数据。

我不知所措。

下面的代码将网页作为流获取,但没有获取ajax调用返回的数据。我正在使用C#.Net Framework 4.5.2

using System;
using System.IO;
using System.Net;
using System.Text;

namespace HttpWebRequestTest
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpWebRequest req;
            HttpWebResponse res = null;

            try
            {
                req = (HttpWebRequest)WebRequest.Create(
                        "http://localhost/WebApp/");
                res = (HttpWebResponse)req.GetResponse();
                Stream stream = res.GetResponseStream();

                byte[] data = new byte[8096];
                int read;
                while ((read = stream.Read(data, 0, data.Length)) > 0)
                {
                    Process(data, read);
                }
            }
            finally
            {
                if (res != null)
                    res.Close();
            }
            Console.In.Read();
        }

        private static void Process(byte[] data, int read)
        {
            Console.Out.Write(ASCIIEncoding.ASCII.GetString(data));
        }
    }
}

0 个答案:

没有答案