如何读取响应流的HTTP响应完成之前 [英] How to read the response stream before the Http response completes

查看:185
本文介绍了如何读取响应流的HTTP响应完成之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用HttpWebRequest对象发出请求时,我需要调用该方法的GetResponse()发送请求并得到响应回来。结果
这种方法的问题是,它不会返回响应对象,直到所有数据已被接收。说我是下载一个100 MB的文件,我将无法读取它,直到响应完成,所有的100 MB的下载。结果
我想要的是能够读取响应流的字节一旦他们到达时,无需等待完成响应。结果
我知道我可以使用的范围HTTP头,但它不会对我的情况下工作。


< DIV CLASS =h2_lin>解决方案

我觉得这是非常接近到什么@Zachary建议。而且(似乎)工作(S);其实我觉得用使用作为@Zachary做甚至是更好。结果
我主要存在我不能看到<$ C $的阻塞行为。C>的GetResponse()你(好像)描述



此外,下面的代码只能大致说明一切是如何工作的;的它不会读取流到结束的例如(除非巧合:))。但是,如果你复制正将其粘贴到Visual Studio中的一个空的控制台应用程序 - 项目它应该工作。



您可以尝试使用一些短的URL一个测试。这里的例子开始下载Debian发行版(有点超过600兆字节)的ISO。 对不起Debian的,我不是故意要偷你的带宽的 - > BTW:?是有什么明智的人可以用它来测试这种情况下



代码的强烈 C#的启发 - 如何通过HTTP 读取XML的连续流。

 使用命名空间StreamReadWebRequest 
{
系统;
使用System.Collections.Generic;
使用System.Text;使用System.Net
;
:使用System.IO;

类节目
{
静态无效的主要(字串[] args)
{
HttpWebRequest的REQ;
HttpWebResponse RES = NULL;


{
REQ =(HttpWebRequest的)WebRequest.Create(
http://cdimage.debian.org/debian-cd/5.0.4/ I386 / ISO-CD / Debian的504-I386-CD-1.ISO);
解析度=(HttpWebResponse)req.GetResponse();
流流= res.GetResponseStream();

字节[]数据=新的字节[4096];
INT读;
而((读取= stream.Read(数据,0,data.Length))大于0)
{
方法(数据读);
}
}
终于
{
如果(RES!= NULL)
res.Close();
}
Console.In.Read();
}

私有静态无效过程(字节[]数据,诠释读取)
{
Console.Out.Write(ASCIIEncoding.ASCII.GetString(数据)) ;
}
}
}


When making a request using HttpWebRequest object, I need to call the method GetResponse() to send the request and get the response back.
The problem with this method is that it doesn't return the response object until all data has been received. Say I am downloading a 100 MB file, I won't be able to read it until the response finish and all the 100 MB is downloaded.
What I want is to be able to read the response stream bytes as soon as they arrive, without waiting for the response to complete.
I know I can use the Range Http header, but it won't work on my situation.

解决方案

I think this is very close to what @Zachary suggests. And it (seems to) work(s); actually I think using using as @Zachary does is even "nicer".
My main point being I cannot see the blocking behaviour of GetResponse() you (seem to) describe.

In addition the following code only roughly shows how everything works; it will not read the stream to the end for example (unless by coincidence :)). But it should work if you copy-n-paste it into an empty "Console Application"-project in Visual Studio.

You can try using some "shorter" URL for a test. The example here starts downloading an ISO of the debian distribution (a bit more than 600 MByte). Sorry debian, I did not mean to steal your bandwidth. -> Btw: is there something sensible one can use to test such a scenario?

The Code is strongly inspired by C# - How to read a continuous stream of XML over HTTP.

namespace StreamReadWebRequest
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.IO;

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

            try
            {
                req = (HttpWebRequest)WebRequest.Create(
                        "http://cdimage.debian.org/debian-cd/5.0.4/i386/iso-cd/debian-504-i386-CD-1.iso");
                res = (HttpWebResponse)req.GetResponse();
                Stream stream = res.GetResponseStream();

                byte[] data = new byte[4096];
                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));
        }
    }
}

这篇关于如何读取响应流的HTTP响应完成之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆