简体   繁体   English

在apache http客户端中,如何将StringBody中的Content-Type保留为空或null?

[英]In apache http client, how to keep the Content-Type in a StringBody as empty or null?

We have a restful API (which we cannot change) where if the Content-Type value for a part is null, we have a specific processing. 我们有一个宁静的API(无法更改),如果零件的Content-Type值为null,我们将进行特定处理。

Until httpclient3.x, we could do a the following and set the content type as null. 在httpclient3.x之前,我们可以执行以下操作并将内容类型设置为null。

StringPart sp = new StringPart("name, "value")
sp.setContentType(null);

Now, we have moved to http components (httpclient4.x jar) and I realised that we need to pass an instance of ContentBody (StringBody to be precise). 现在,我们转到了http组件(httpclient4.x jar),我意识到我们需要传递ContentBody的实例(准确地说是StringBody)。 Found the following 2 ways of doing the same in the new version: 在新版本中找到以下两种相同的方法:

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")));

However, I couldn't pass the body with no Content-Type. 但是,我无法通过没有Content-Type的正文。 Even if I use the deprecated constructor for StringBody, it sets the Content-Type to plain-text. 即使我对StringBody使用了不推荐使用的构造函数,它也会将Content-Type设置为纯文本。

I know that any part of a request should always have a Content-Type but this is a special case of an outdated destination server which we cannot upgrade. 我知道请求的任何部分都应该始终具有Content-Type,但这是过时的目标服务器的特殊情况,我们无法对其进行升级。

I think your only option is to use deprecated FormBodyPart#FormBodyPart(String name, ContentBody) constructor AND to override deprecated FormBodyPart#generateContentType method. 我认为您唯一的选择是使用不赞成使用的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);

std 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.

相关问题 在 Apache 中获取 Http 主体,如果未提供内容类型,骆驼将无法正常工作 - Get Http body in Apache Camel not working if content-type not presented 使用Jersey客户端下载文件时内容为空 - Empty content-type while downloading file with Jersey client 如果客户端设置 content-type="application/xml" 并且 body=null,如何解析 POST 请求的请求正文? - How to parse request body of POST request, if client set content-type="application/xml" and body=null? Apache CXF拦截器覆盖内容类型 - Apache CXF interceptor override content-type Spring-更改内容类型HTTP标头 - Spring - changing Content-type HTTP header class org.springframework.http.converter.HttpMessageNotWritableException 没有转换器 [class java.lang.Boolean] 与预设 Content-Type 'null' - class org.springframework.http.converter.HttpMessageNotWritableException No converter for [class java.lang.Boolean] with preset Content-Type 'null' 打开HTTP连接后如何识别内容类型? - How can I identify the content-type after opening an HTTP connection? 如何声明由SpringFramework的org.springframework.http.ResponseEntity类设置的“ Content-Type”? - How to assert “Content-Type” set by SpringFramework's org.springframework.http.ResponseEntity class? 如何强制HTTP请求连接器使用特定的Content-Type? - How do I force the HTTP Request Connector to use a certain Content-Type? 如何在IBM HTTPD服务器上设置Content-Type HTTP标头的字符集部分? - How do I set the charset portion of the Content-Type HTTP Header on an IBM HTTPD Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM