HTTP 400 当执行来自 Apache HTTP 客户端的多部分请求时
HTTP 400 When executing a multi part request from Apache HTTP Client
我正在尝试将来自 Servlet 的请求转发到 JAX-RS 运行 的 CXF 实现中实现的实际后端 tomcat 7.
以下是我的代码,
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
HttpPost postRequest = new HttpPost(targetURL);
if (ServletFileUpload.isMultipartContent(req)) {
ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
List<FileItem> fileItemList = servletFileUpload.parseRequest(req);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
for (FileItem item: fileItemList) {
entityBuilder.addPart(item.getFieldName(), new ByteArrayBody(item.get(), ContentType.create(item.getContentType()), item.getName()));
}
postRequest.setEntity(entityBuilder.build());
} else {
if (StringUtils.isNotEmpty(req.getHeader(HttpHeaders.CONTENT_LENGTH)) || StringUtils.isNotEmpty(req.getHeader(HttpHeaders.TRANSFER_ENCODING))) {
InputStreamEntity entity = new InputStreamEntity(req.getInputStream(), Long.parseLong(req.getHeader(HttpHeaders.CONTENT_LENGTH)));
postRequest.setEntity(entity);
}
}
postRequest.setHeader(postRequest.getEntity().getContentType());
postRequest.setHeader(HttpHeaders.AUTHORIZATION, "ACCESS-TOKEN");
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpResponse response = client.execute(httpRequest);
}
}
以上代码运行 non-multipart 请求没有问题。但是,每当我尝试多部分请求时,都会收到 "HTTP 400 Bad Request" 响应。请求也没有传递到实际实现(后端没有错误)
HttpResponseProxy{HTTP/1.1 400 Bad Request [X-Frame-Options: DENY, X-Content-Type-Options: nosniff, X-XSS-Protection: 1; mode=block, Access-Control-Allow-Origin: *, Access-Control-Allow-Methods: GET, POST, DELETE, PUT, Access-Control-Allow-Headers: Content-Type, Date: Fri, 21 Jun 2019 01:27:59 GMT, Content-Length: 0, Connection: close] [Content-Length: 0,Chunked: false]}
我试图用原始边界保留传入的content-type header,然后后端抛出以下错误。
Caused by: org.apache.cxf.interceptor.Fault: Couldn't find MIME boundary: ----------------------------282428552532940822247758
at org.apache.cxf.interceptor.AttachmentInInterceptor.handleMessage(AttachmentInInterceptor.java:60)
at org.apache.cxf.jaxrs.ext.MessageContextImpl.createAttachments(MessageContextImpl.java:268)
at org.apache.cxf.jaxrs.ext.MessageContextImpl.get(MessageContextImpl.java:77)
知道我做错了什么吗?
postRequest.setHeader(postRequest.getEntity().getContentType());
不要那样做。 HttpClient 根据所附HttpEntity
的属性自动为请求消息body 生成Content-Type
header。
我正在尝试将来自 Servlet 的请求转发到 JAX-RS 运行 的 CXF 实现中实现的实际后端 tomcat 7.
以下是我的代码,
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
HttpPost postRequest = new HttpPost(targetURL);
if (ServletFileUpload.isMultipartContent(req)) {
ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
List<FileItem> fileItemList = servletFileUpload.parseRequest(req);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
for (FileItem item: fileItemList) {
entityBuilder.addPart(item.getFieldName(), new ByteArrayBody(item.get(), ContentType.create(item.getContentType()), item.getName()));
}
postRequest.setEntity(entityBuilder.build());
} else {
if (StringUtils.isNotEmpty(req.getHeader(HttpHeaders.CONTENT_LENGTH)) || StringUtils.isNotEmpty(req.getHeader(HttpHeaders.TRANSFER_ENCODING))) {
InputStreamEntity entity = new InputStreamEntity(req.getInputStream(), Long.parseLong(req.getHeader(HttpHeaders.CONTENT_LENGTH)));
postRequest.setEntity(entity);
}
}
postRequest.setHeader(postRequest.getEntity().getContentType());
postRequest.setHeader(HttpHeaders.AUTHORIZATION, "ACCESS-TOKEN");
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpResponse response = client.execute(httpRequest);
}
}
以上代码运行 non-multipart 请求没有问题。但是,每当我尝试多部分请求时,都会收到 "HTTP 400 Bad Request" 响应。请求也没有传递到实际实现(后端没有错误)
HttpResponseProxy{HTTP/1.1 400 Bad Request [X-Frame-Options: DENY, X-Content-Type-Options: nosniff, X-XSS-Protection: 1; mode=block, Access-Control-Allow-Origin: *, Access-Control-Allow-Methods: GET, POST, DELETE, PUT, Access-Control-Allow-Headers: Content-Type, Date: Fri, 21 Jun 2019 01:27:59 GMT, Content-Length: 0, Connection: close] [Content-Length: 0,Chunked: false]}
我试图用原始边界保留传入的content-type header,然后后端抛出以下错误。
Caused by: org.apache.cxf.interceptor.Fault: Couldn't find MIME boundary: ----------------------------282428552532940822247758
at org.apache.cxf.interceptor.AttachmentInInterceptor.handleMessage(AttachmentInInterceptor.java:60)
at org.apache.cxf.jaxrs.ext.MessageContextImpl.createAttachments(MessageContextImpl.java:268)
at org.apache.cxf.jaxrs.ext.MessageContextImpl.get(MessageContextImpl.java:77)
知道我做错了什么吗?
postRequest.setHeader(postRequest.getEntity().getContentType());
不要那样做。 HttpClient 根据所附HttpEntity
的属性自动为请求消息body 生成Content-Type
header。