[英]Dropwizard Jersey client Multipart http request
Dropwizard (Version 0.8.2) uses Jersey internally to provide HTTP client. Dropwizard(版本0.8.2)在内部使用Jersey提供HTTP客户端。 I am using this client to send a Multipart POST request to an external Rest Endpoint to a SMS Service.
我正在使用此客户端将Multipart POST请求发送到SMS服务的外部Rest Endpoint。 Code is given below but it doesn't seems to be working because i am not receiving any message through this method also it does not throw any error.
下面给出了代码,但是它似乎不起作用,因为我没有通过此方法接收任何消息,它也不会引发任何错误。 URI for the first sample is http://enterprise.com/GatewayAPI/rest?userid=%s&password=%s&method=xlsUpload&filetype=zip&msg_type=TEXT&auth_scheme=PLAIN&v=1.1
第一个示例的URI是http://enterprise.com/GatewayAPI/rest?userid=%s&password=%s&method=xlsUpload&filetype=zip&msg_type=TEXT&auth_scheme=PLAIN&v=1.1
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart(fileName, file,
MediaType.APPLICATION_OCTET_STREAM_TYPE);
FormDataMultiPart multiPart = new FormDataMultiPart();
multiPart.field("fileName", fileName).bodyPart(fileDataBodyPart);
Entity<FormDataMultiPart> entity =
Entity.entity(multiPart, multiPart.getMediaType());// MediaType.MULTIPART_FORM_DATA_TYPE)
Client tenacityClient = TenacityJerseyClientBuilder
.builder(AppDependencyKeys.BULK_SMS)
.usingTimeoutPadding(Duration.milliseconds(500)).build(client)
.register(MultiPartFeature.class);
Invocation invocation = getResourceBuilder(tenacityClient, uri).buildPost(entity);
Future<Response> futureResponse = invocation.submit();
long start = System.currentTimeMillis();
futureResponse.get();
But the same works with below method when i use Apache Commons Httpclient. 但是当我使用Apache Commons Httpclient时,以下方法也可以使用。 working code for the same is given below.
相同的工作代码如下。
HttpClient client = new HttpClient();
PostMethod method = new
PostMethod("http://enterprise.com/GatewayAPI/rest");
Part[] parts = {
new StringPart("method", "xlsUpload"),
new StringPart("userid", "*******"),
new StringPart("password", "*******"),
new StringPart("filetype", "zip"),
new StringPart("v", "1.1"),
new StringPart("auth_scheme", "PLAIN"),
new FilePart(file.getName(), file)
};
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
int statusCode = client.executeMethod(method);
log.info("Status code: {}", statusCode);
But i want to use the first way as that suits my infrastructure better. 但是我想使用第一种方法,因为它更适合我的基础结构。
I think you should set up properly media type for entity. 我认为您应该为实体正确设置媒体类型。 Currently, you created new FormDataMultiPart but, you did not set and media type and it uses "text/plain" y default.
当前,您创建了新的FormDataMultiPart,但是尚未设置和设置媒体类型,并且默认使用“文本/纯文本”。
So, you should set up MediaType.APPLICATION_OCTET_STREAM_TYPE to your FormDataMultiPart as media type. 因此,您应该将MediaType.APPLICATION_OCTET_STREAM_TYPE设置为FormDataMultiPart作为媒体类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.