上传大文件时的Apache的HttpClient 4.x的行为很奇怪吗? [英] Apache HttpClient 4.x behaving strange when uploading larger files?

查看:1047
本文介绍了上传大文件时的Apache的HttpClient 4.x的行为很奇怪吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发和测试使用Java(和Scala)有点直接的客户端 - 服务器应用程序。

I'm developing and testing a little straight-forward client-server application using java (and scala).

服务器是根据 com.sun.net.httpserver.HttpServer ,并允许文件上传通过使用POST一个基本的RESTful接口和PUT操作。上传操作是使用了我们实现我们自己的摘要式身份验证限制,测试和工程浏览器,卷曲和的Apache的HttpClient

The server is based on com.sun.net.httpserver.HttpServer and allows the upload of files via a basic RESTful interface using POST and PUT operations. The upload operation is restricted using Digest authentication which we implemented by ourselves, is tested and works in browsers, curl and Apache HttpClient.

上传的客户端包裹的Apache的HttpClient 4.1.2 并通过HTTP PUT执行操作上载文件的实体。内容类型的文件被指定为应用程序/ XML 在页眉和只有一个文件,同时上传。

The upload client wraps Apache HttpClient 4.1.2 and executes PUT operations over http to upload file entities. The content-type of the file is specified as application/xml in the header and only a single file is uploaded at a time.

当上传档案的不同尺寸一个奇怪的行为可以观察到:

When uploading files of different sizes a strange behaviour could be observed:


  • 与尺寸更小的文件或等于1.076.006字节被上传
    成功

  • 文件与尺寸大于或等于1.122.158字节
    失败 java.net.SocketException异常:残破的管道

  • Files with sizes less or equals to 1.076.006 Byte are uploaded successfully.
  • Files with sizes greater or equals to 1.122.158 Bytes fail with a java.net.SocketException: Broken pipe.

准确的临界大小是未知的,因为我已经创建的文件与手动不同大小近似最大加工尺寸的)

究其原因,破裂的管道是,客户端某种程度上忽略了 WWW验证 - 响应上传的大小的文件,为的是服务器日志记录。 忽​​略的意思,它只是发送多个(4)不含认证头的所有消息。
但是的更小的文件的运作良好,客户端发送与适当的挑战 - 响应正确后,立即 WWW验证 - 响应,因为它应该认证请求可以。

The reason for the broken pipe is, that the client somehow ignored the www-authenticate-response uploading files of that size, as is documented by the server logs. "Ignore" means, that it just send multiple (4) messages containing no authentication header at all. But smaller files work well and the client sends an authentication request with the proper challenge-response correctly immediately after the www-authenticate-response as it should be.

上传的卷曲适用于各种规模的文件,所以没有问题存在。

所以在这一点上,我们可以说:有你的客户端的一些bug。好吧,我有点希望如此,但是我也试过一个开源Java的 RESTClient实现 (另包阿帕奇HttpClient的),它具有的究竟的相同的行为!

So at this point, one could say: "There is some bug in your client." Okay, I kind of hope so, but I've also tried an open-source java RESTclient (also wrapping apache httpclient) and it has exactly the same behaviour!

我们尝试了使用该客户端在互联网上,它也一样描述。所以现在,我只希望我错过了设置一些重要的事情在的Apache的HttpClient 从而导致这种错误行为和开源RESTClient实现的开发商错过了它,以及...任何想法是什么可能将是伟大的!

We tried it using this client over the internet and its also the same as described. So right now, I just hope I've missed to set something important in Apache HttpClient which leads to this erroneous behaviour and the developer of the open-source RESTclient missed it as well... any ideas what it could be would be great!

推荐答案

最有可能是导致这种局面的几个因素的结合

Most likely it is a combination of several factors that leads to this situation

(1)最有可能你的客户不使用'期望继续'与不包括认证头的请求发送大的请求实体时握手。

(1) Most likely your client does not use the 'expect-continue' handshake when sending large request entity with a request that does not include an authentication header.

(2)服务器的早期检测的请求失败的期望和阅读,而不是和全丢弃请求体它与早期的401状态响应,并关闭其端部连接。在我看来,这是一个HTTP协议违反了服务器的一部分。

(2) The server detects early that the request fails its expectations and instead of reading and discarding full request body it responds early with 401 status and closes connection on its end. In my opinion, this is an HTTP protocol violation on the part of the server.

(3)虽然有些HTTP代理可以处理的早期反应,阿帕奇的HttpClient不能由于Java的阻塞I / O(执行线程可以读取或阻塞套接字写,但不能同时)的限制。

(3) While some HTTP agents can deal with early responses, Apache HttpClient cannot due to the limitation of the Java blocking I/O (a thread of execution can either read or write from a blocking socket, but not both).

有解决的问题,预计-继续握手是最简单,最自然的多种方式。或者可以执行一个简单的HEAD或GET请求强制HTTP验证执行大POST或PUT请求之前。 HttpClient的是能够重复使用的认证数据为在同一逻辑HTTP会话的后续请求的。

There are multiple ways of addressing the issue, the 'expect-continue' handshake being the easiest and most natural one. Alternatively one can execute a simple HEAD or a GET request to force HTTP authentication prior to executing a large POST or PUT request. HttpClient is capable of re-using authentication data for subsequent requests in the same logical HTTP session.

这篇关于上传大文件时的Apache的HttpClient 4.x的行为很奇怪吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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