当我使用httpclient 4.4发出多部分HTTP请求时:
HttpClientBuilder hcBuilder = HttpClientBuilder.create();
RequestBuilder reqBuilder = RequestBuilder.post();
MultipartEntityBuilder meb = MultipartEntityBuilder.create();
ContentType contentType = ...;
String body = ...;
StringBody sb = new StringBody(body, contentType);
reqBuilder.setEntity(meb.build());
HttpUriRequest req = reqBuilder.build();
httpClient = hcBuilder.build();
HttpResponse http_res = httpClient.execute(req);
请求构建为:
POST / HTTP/1.1
Content-Length: 123
Content-Type: multipart/form-data; boundary=4gzd6246Ar7-0tL4z8wXxJFc3qeR8_GaSoQtF
Host: localhost:10101
User-Agent: Apache-HttpClient/4.4 (java 1.5)
Accept-Encoding: gzip,deflate
--4gzd6246Ar7-0tL4z8wXxJFc3qeR8_GaSoQtF
Content-Disposition: form-data; name="str-name"
Hello World
--4gzd6246Ar7-0tL4z8wXxJFc3qeR8_GaSoQtF--
使用httpclient 4.4,有没有办法将部件的Content-Disposition
标头值设置为任意值(httpclient生成的默认值为form-data
)?
答案 0 :(得分:2)
FormBodyPart bodyPart = FormBodyPartBuilder.create()
.setName("blah")
.addField("Content-Disposition", "blah")
.setBody(new StringBody("blah", ContentType.TEXT_PLAIN))
.build();
HttpEntity entity = MultipartEntityBuilder.create()
.addPart(bodyPart)
.build();
entity.writeTo(System.out);
产生
--nlHhbHgLGuf9bkSZJ1wR6aSFn5tT6K
Content-Disposition: blah
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
blah
--nlHhbHgLGuf9bkSZJ1wR6aSFn5tT6K--