PostMethod setRequestBody(String) deprecated - why?

元气小坏坏 提交于 2019-11-27 02:30:38

问题


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?


回答1:


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:

  • if your xml is in a String, use the StringRequestEntity
  • if it is in a file, use the FileRequestEntity

and so on.




回答2:


Yes, so for example,

post.setRequestEntity( new StringRequestEntity( xml ) );

instead of

post.setRequestBody( xml );


来源:https://stackoverflow.com/questions/2092474/postmethod-setrequestbodystring-deprecated-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!