views:

296

answers:

2

I'm a newbie to http and Apache's HttpComponents API.

I need to process a streaming response of an http request using Apache's HttpComponents, while there may be additional http requests made on that connection. For example, a POST request is initially made to http://mystreams.net, which later follows with additional requests, while throughout I have to listen and process the streaming response. I need to hold the same initial connection I made.

How can I do that? I was able to create a simple HttpClient and do a simple HttpPost request and then process the response entity that is non-streaming, but how do I hold on to it when it continues to stream data and at the same time make new requests to the same address using the same context (ie cookies)?

A: 

Is your streaming data coming back as a single HTTP response? If so, you won't be able to receive other responses on that connection until it is done. But you could take the cookies from that response (while it is still streaming the entity to you) and use them to make other requests on another connection.

Harold L
> Is your streaming data coming back as a single HTTP response?Yes. Since it's all sockets I thought that the response to a new request could come back on an existing socket that is listened to.
andrewz
A new request can be sent on the socket, but per the HTTP spec you won't get the response until the prior response has completed.
Harold L
That's fine. But I *can* get a response on the first socket? How do I do this in HttpComponents?
andrewz
A: 
  • HttpEntity entity = httpclient.execute(httpget).getEntity();
  • InputStream is = entity.getContent()
  • when calling the stream, use a new Thread, and make subsequent requests in the main thread (or, better, in separate thread for reach)

Also check here

Bozho
I see no PostMethod class in the class index for HttpComponents.
andrewz
sorry, that was for the old version - check my update
Bozho
So are you saying then that if I hold on to the 'InputStream' returned from 'entity.getContent()' of the first call that I will be able to receive response from a 2nd call in that stream?
andrewz
No. You will receive the streaming response for this request. Other requests should be made from different threads, and different responses will be received.
Bozho