![](/img/trans.png)
[英]Get Http body in Apache Camel not working if content-type not presented
[英]In apache http client, how to keep the Content-Type in a StringBody as empty or null?
我們有一個寧靜的API(無法更改),如果零件的Content-Type值為null,我們將進行特定處理。
在httpclient3.x之前,我們可以執行以下操作並將內容類型設置為null。
StringPart sp = new StringPart("name, "value")
sp.setContentType(null);
現在,我們轉到了http組件(httpclient4.x jar),我意識到我們需要傳遞ContentBody的實例(准確地說是StringBody)。 在新版本中找到以下兩種相同的方法:
multipartEntityBuilder.addPart(FormBodyPartBuilder.create().setName(propName).setBody(new StringBody(propValue, ContentType.create("application/octet-stream", "UTF-8"))).build());
multipartEntityBuilder.addPart(propName, new StringBody(propValue, ContentType.create("application/octet-stream", "UTF-8")));
但是,我無法通過沒有Content-Type的正文。 即使我對StringBody使用了不推薦使用的構造函數,它也會將Content-Type設置為純文本。
我知道請求的任何部分都應該始終具有Content-Type,但這是過時的目標服務器的特殊情況,我們無法對其進行升級。
我認為您唯一的選擇是使用不贊成使用的FormBodyPart#FormBodyPart(String name, ContentBody)
構造函數,並重寫不贊成使用的FormBodyPart#generateContentType
方法。
FormBodyPart bodyPart = new FormBodyPart("stuff", new StringBody("stuff", ContentType.DEFAULT_TEXT)) {
@Override
protected void generateContentType(ContentBody body) {
}
};
HttpEntity entity = MultipartEntityBuilder.create().addPart(bodyPart).build();
entity.writeTo(System.out);
標准輸出>
--yJHqjnFj0u-dWGPGkGtK_NnOgUeOspQ
Content-Disposition: form-data; name="stuff"
Content-Transfer-Encoding: 8bit
stuff
--yJHqjnFj0u-dWGPGkGtK_NnOgUeOspQ--
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.