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.
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.
There is a specific comment about this in the Twitter documentation:
"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."
They point to the Appache HttpClient, but that doesn't help much because I need to use .NET.
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
BeginGetResponse is the method you need. It allows you to read the response stream incrementally:
Or if you feel more comfortable with WebClient (I personnally prefer it over WebRequest):
Just use
WebClient
. It is designed for simple cases like this where you don't need the full power of WebRequest.Have you tried WebRequest.BeginGetRequestStream() ?
Or something like this:
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: