本文介绍了Apache HTTPClient 流式传输 HTTP POST 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Apache HTTPClient.

I'm trying to build a "full-duplex" HTTP streaming request using Apache HTTPClient.

在我的第一次尝试中,我尝试使用以下请求代码:

In my first attempt, I tried using the following request code:

URL url=new URL(/* code goes here */);

HttpPost request=new HttpPost(url.toString());

request.addHeader("Connection", "close");

PipedOutputStream requestOutput=new PipedOutputStream();
PipedInputStream requestInput=new PipedInputStream(requestOutput, DEFAULT_PIPE_SIZE);
ContentType requestContentType=getContentType();
InputStreamEntity requestEntity=new InputStreamEntity(requestInput, -1, requestContentType);
request.setEntity(requestEntity);

HttpEntity responseEntity=null;
HttpResponse response=getHttpClient().execute(request); // <-- Hanging here
try {
    if(response.getStatusLine().getStatusCode() != 200)
        throw new IOException("Unexpected status code: "+response.getStatusLine().getStatusCode());

    responseEntity = response.getEntity();
}
finally {
    if(responseEntity == null)
        request.abort();
}

InputStream responseInput=responseEntity.getContent();
ContentType responseContentType;
if(responseEntity.getContentType() != null)
    responseContentType = ContentType.parse(responseEntity.getContentType().getValue());
else
    responseContentType = DEFAULT_CONTENT_TYPE;

Reader responseStream=decode(responseInput, responseContentType);
Writer requestStream=encode(requestOutput, getContentType());

请求挂在上面指示的行上.代码似乎试图在获得响应之前发送整个请求.回想起来,这是有道理的.然而,这并不是我所希望的.:)

The request hangs at the line indicated above. It seems that the code is trying to send the entire request before it gets the response. In retrospect, this makes sense. However, it's not what I was hoping for. :)

相反,我希望使用 Transfer-Encoding: chunked 发送请求标头,使用 Transfer 接收 HTTP/1.1 200 OK 的响应标头-Encoding:chunked 自己的标头,然后我有一个全双工流式 HTTP 连接可以使用.

Instead, I was hoping to send the request headers with Transfer-Encoding: chunked, receive a response header of HTTP/1.1 200 OK with a Transfer-Encoding: chunked header of its own, and then I'd have a full-duplex streaming HTTP connection to work with.

令人高兴的是,我的 HTTPClient 有另一个基于 NIO 的异步客户端,有很好的使用示例(例如 这个).我的问题是:

Happily, my HTTPClient has another NIO-based asynchronous client with good usage examples (like this one). My questions are:

  1. 我对同步 HTTPClient 行为的解释是否正确?或者我可以做些什么来继续以我描述的方式使用(更简单的)同步 HTTPClient?
  2. 基于 NIO 的客户端是否在寻求响应之前等待发送整个请求?或者,我能否在增量发送请求的同时增量接收响应?

如果 HTTPClient 不支持这种模式,是否还有其他 HTTP 客户端库支持?或者我应该计划编写一个(最小的)HTTP 客户端来支持这种模式?

If HTTPClient will not support this modality, is there another HTTP client library that will? Or should I be planning to write a (minimal) HTTP client to support this modality?

推荐答案

这是我对略读代码的看法:

Here is my view on skim reading the code:

  1. 我不能完全同意非 200 响应意味着失败这一事实.所有 2XX 响应大多是有效的.查看 wiki 了解更多详情

对于任何 TCP 请求,我建议接收整个响应以确认其有效.我这样说是因为,部分响应可能主要被视为不良响应,因为大多数客户端实现无法使用它.(想象一下服务器响应 2MB 数据的情况,但在此期间它宕机了)

For any TCP request, I would recommend to receive the entire response to confirm that it is valid. I say this because, a partial response may mostly be treated as bad response as most of the client implementations cannot make use of it. (Imagine a case where server is responding with 2MB of data and it goes down during this time)

这篇关于Apache HTTPClient 流式传输 HTTP POST 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:28