[英]Dropwizard Jersey client Multipart http request
Dropwizard(版本0.8.2)在內部使用Jersey提供HTTP客戶端。 我正在使用此客戶端將Multipart POST請求發送到SMS服務的外部Rest Endpoint。 下面給出了代碼,但是它似乎不起作用,因為我沒有通過此方法接收任何消息,它也不會引發任何錯誤。 第一個示例的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();
但是當我使用Apache Commons Httpclient時,以下方法也可以使用。 相同的工作代碼如下。
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);
但是我想使用第一種方法,因為它更適合我的基礎結構。
我認為您應該為實體正確設置媒體類型。 當前,您創建了新的FormDataMultiPart,但是尚未設置和設置媒體類型,並且默認使用“文本/純文本”。
因此,您應該將MediaType.APPLICATION_OCTET_STREAM_TYPE設置為FormDataMultiPart作為媒體類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.