PostMethod setRequestBody(String) deprecated - why

2019-01-18 01:27发布

I am using Apache Commons HttpClient PostMethod 3.1.

In the PostMethod class there are also three methods for setting POST method's request body:

setRequestBody(InputStream body)
setRequestBody(String body)
setRequestBody(NameValuePair[] parametersBody);

NameValuePair API

First two methods are deprecated. Does anybody knows why? Because if I want to put an XML to request body, NameValuePair does not help me.

Does anybody knows an workaround or a solution?

2条回答
对你真心纯属浪费
2楼-- · 2019-01-18 02:19

The javadoc says:

Deprecated. use setRequestEntity(RequestEntity)

RequestEntity has a lot of implementors, namely:

ByteArrayRequestEntity, FileRequestEntity, InputStreamRequestEntity, MultipartRequestEntity, StringRequestEntity

Use the one that suits you:

and so on.

查看更多
欢心
3楼-- · 2019-01-18 02:22

Yes, so for example,

post.setRequestEntity( new StringRequestEntity( xml ) );

instead of

post.setRequestBody( xml );
查看更多
登录 后发表回答