HTTP Put设置内容类型 [英] HTTP Put set content type

查看:160
本文介绍了HTTP Put设置内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置HTTP Put的内容类型为xxxx + xml?

How can I set content type of HTTP Put as xxxx+xml?

我指的是此链接中的解决方案 Android,通过HTTP POST(SOAP)发送XML 。我们设置这样的内容类型时很好,我的意思是xml随请求一起出现:

I was referring to solution in this link Android, sending XML via HTTP POST (SOAP). Its fine when we set content type like this, i mean the xml is came along with the request:

httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");

但是当我将类型 soap 更改为自定义内容时,xml会消失请求(我在wireshark上看到),如下所示:

but when i change type soap to something custom, the xml disappear on the request (i saw on the wireshark), like this:

httppost.setHeader("Content-Type","application/vnd.oma-pcc+xml;charset=UTF-8");

然后,我尝试只放xml,所以请求再次正常:

then, i tried put the xml only, so the request is ok again:

httppost.setHeader("Content-Type","application/xml;charset=UTF-8");

我想知道内容类型的确切规则是什么,而不是与xml类型一起那个xml还在那里。

I want to know what exactly the rules for the content-type than come together with the xml type so that the xml still there.

谢谢。

推荐答案

假设你'使用4.1.3或更高版本的HTTPClient -

Assuming you're using HTTPClient of 4.1.3 or greater -

构建实体时,您可以选择指定用于POST或PUT操作的内容某些实体。

When constructing you're entity, you have the option to specify the content being used for the POST or PUT operation for certain entities.

有一个 ContentType 对象,应该用来指定它。

There is a ContentType object which should be used to specify this.

使用工厂方法.create(你可以使用charset指定mimetype - 框架将使用ContentType来正确地发出相关标题。

Using the factory method .create() you can specify the mimetype with a charset - the ContentType will be used by the framework to properly emit the header in question.

示例API调用:

ContentType.create(application / vnd.oma-pcc + xml,CharSet.forName(UTF-8));

注意编辑HttpClient 4.1.2

对于4.1.2 ,当您为帖子或放置操作创建实体时,请在 实体 上设置内容类型,而不是 执行 (HttpPost或HttpPut)使用setContentType(String)。这在4.1.3及更高版本中已弃用。

In the case of 4.1.2, when you create your entity for the post or put operation, set the content type on the entity not the execution (HttpPost or HttpPut) using setContentType(String). This is deprecated in 4.1.3 and beyond.

这篇关于HTTP Put设置内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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