简体   繁体   English

使用httpcomponents处理流式响应时出现多个http请求

[英]multiple http requests when processing streaming response using httpcomponents

I'm a newbie to http and Apache's HttpComponents API. 我是http和Apache的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. 我需要使用Apache的HttpComponents处理HTTP请求的流式响应,而在该连接上可能还会有其他HTTP请求。 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. 例如,最初会向http://mystreams.net发出POST请求,随后会发出其他请求,而在整个过程中,我必须侦听和处理流式响应。 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)? 我能够创建一个简单的HttpClient并执行一个简单的HttpPost请求,然后处理非流式处理的响应实体,但是当它继续流式传输数据并同时向该对象发出新请求时,我该如何保留它呢?使用相同上下文(例如Cookie)的相同地址?

Is your streaming data coming back as a single HTTP response? 您的流数据是否作为单个HTTP响应返回? 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. 但是,您可以从该响应中获取Cookie(尽管它仍在将实体发送给您),然后使用它们在另一个连接上发出其他请求。

  • 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 也检查这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM