在apache http客户端中,如何将StringBody中的Content-Type保留为空或null? [英] In apache http client, how to keep the Content-Type in a StringBody as empty or null?

查看:480
本文介绍了在apache http客户端中,如何将StringBody中的Content-Type保留为空或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)构造函数 AND 来覆盖已弃用的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--

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.

Until httpclient3.x, we could do a the following and set the content type as 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). 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. Even if I use the deprecated constructor for StringBody, it sets the Content-Type to plain-text.

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.

解决方案

I think your only option is to use deprecated FormBodyPart#FormBodyPart(String name, ContentBody) constructor AND to override deprecated FormBodyPart#generateContentType method.

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

这篇关于在apache http客户端中,如何将StringBody中的Content-Type保留为空或null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆