Jersey Client 2代理和多部分支持

时间:2015-08-10 15:32:19

标签: apache-httpclient-4.x multipartform-data jersey-2.0 multipart jersey-client

我正在尝试向我的Jersey客户端添加代理支持。 我使用org.glassfish.jersey.core:jersey-client:2.11但我可以切换到任何Jersey Client 2版本。 目前,客户端使用默认的Jersey连接器,该连接器不支持代理AFAIK。

我试过这里描述的解决方案How to add a http proxy for Jersey2 Client 但是当我发送多部分内容时,我得到:

org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

和客户端中的警告:

Aug 10, 2015 5:10:32 PM org.glassfish.jersey.message.internal.HeaderUtils checkHeaderChanges
WARNING: There are some request headers that have not been sent by connector [org.glassfish.jersey.apache.connector.ApacheConnector]. Probably you added those headers in WriterInterceptor or MessageBodyWriter. That feature is not supported by the connector. Please, do not modify headers in WriterInterceptor or MessageBodyWriter or use default HttpUrlConnector instead.
Unsent header changes: [MIME-Version, Content-Type]

此外,Jersey Client 2文档也提到了问题(https://jersey.java.net/documentation/latest/user-guide.html#d0e9179

Warning
Do not use ApacheConnectorProvider nor GrizzlyConnectorProvider neither JettyConnectorProvider connector implementations with Jersey Multipart features. See Header modification issue warning for more details.

配置Jersey Client 2以支持两者的方式是什么:

  1. 发送多部分内容(用于在服务器上上传文件)
  2. 代理服务器支持
  3. PS。也许有一种方法可以为默认的jersey连接器实现添加代理支持?

1 个答案:

答案 0 :(得分:7)

我想出了一种使用apache连接器(默认httpurl连接器以外的连接器)以及multipart功能的方法。如果我在使用Invocation.Builder构建请求时向内容类型添加边界,则可以正常工作。

我的代码如下。

MediaType contentType = MediaType.MULTIPART_FORM_DATA_TYPE;
contentType = Boundary.addBoundary(contentType); // import org.glassfish.jersey.media.multipart.Boundary;
Response response = builder.post(Entity.entity(requestPayload, contentType), Response.class);