从打开的HTTP流中读取数据 [英] Reading data from an open HTTP stream

查看:299
本文介绍了从打开的HTTP流中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用.NET WebRequest / WebResponse类来访问Twitter流媒体API http://stream.twitter.com/spritzer.json

I am trying to use the .NET WebRequest/WebResponse classes to access the Twitter streaming API here "http://stream.twitter.com/spritzer.json".

我需要能够打开连接并从打开的连接中逐步读取数据。

I need to be able to open the connection and read data incrementally from the open connection.

目前,当我调用 WebRequest.GetResponse 方法时,它会阻塞,直到整个响应被下载。我知道有一个 BeginGetResponse 方法,但这只会在后台线程上做同样的事情。我需要在下载仍然发生时访问响应流。这对我来说似乎不适合这些课程。

Currently, when I call WebRequest.GetResponse method, it blocks until the entire response is downloaded. I know there is a BeginGetResponse method, but this will just do the same thing on a background thread. I need to get access to the response stream while the download is still happening. This just does not seem possible to me with these classes.

在Twitter文档中有关于此的具体评论:

There is a specific comment about this in the Twitter documentation:

请注意,某些HTTP客户端库仅在服务器关闭连接后才返回响应主体。这些客户端不能用于访问Streaming API。您必须使用将以递增方式返回响应数据的HTTP客户端最强大的HTTP客户端库将提供此功能。例如,Apache HttpClient将处理此用例。

"Please note that some HTTP client libraries only return the response body after the connection has been closed by the server. These clients will not work for accessing the Streaming API. You must use an HTTP client that will return response data incrementally. Most robust HTTP client libraries will provide this functionality. The Apache HttpClient will handle this use case, for example."

他们指向Appache HttpClient,但这并不是'帮助很大,因为我需要使用.NET。

They point to the Appache HttpClient, but that doesn't help much because I need to use .NET.

是否可以使用 WebRequest / WebResponse 进行任何想法,或者我是否需要选择较低级别的网络课程?也许还有其他库可以让我这样做?

Any ideas whether this is possible with WebRequest/WebResponse, or do I have to go for lower level networking classes? Maybe there are other libraries that will allow me to do this?

Thx
Allen

Thx Allen

推荐答案

我最终使用一个TcpClient,工作正常。仍然有兴趣知道这是否可以使用WebRequest / WebResponse。以下是我的代码以防任何人感兴趣:

I ended up using a TcpClient, which works fine. Would still be interested to know if this is possible with WebRequest/WebResponse though. Here is my code in case anybody is interested:

using (TcpClient client = new TcpClient())
{

string requestString = "GET /spritzer.json HTTP/1.1\r\n";
requestString += "Authorization: " + token + "\r\n";
requestString += "Host: stream.twitter.com\r\n";
requestString += "Connection: keep-alive\r\n";
requestString += "\r\n";

client.Connect("stream.twitter.com", 80);

using (NetworkStream stream = client.GetStream())
{
    // Send the request.
    StreamWriter writer = new StreamWriter(stream);
    writer.Write(requestString);
    writer.Flush();

    // Process the response.
    StreamReader rdr = new StreamReader(stream);

    while (!rdr.EndOfStream)
    {
        Console.WriteLine(rdr.ReadLine());
    }
}
}

这篇关于从打开的HTTP流中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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