Java PostMethod setRequestBody(String) 已弃用 - 为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2092474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PostMethod setRequestBody(String) deprecated - why?
提问by Trick
I am using Apache Commons HttpClient PostMethod 3.1.
我正在使用 Apache Commons HttpClient PostMethod 3.1。
In the PostMethod class there are also three methods for setting POST method's request body:
在 PostMethod 类中,还有三种设置 POST 方法的请求体的方法:
setRequestBody(InputStream body)
setRequestBody(String body)
setRequestBody(NameValuePair[] parametersBody);
NameValuePairAPI
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.
NameValuePairAPI
前两个方法已弃用。有人知道为什么吗?因为如果我想将 XML 放在请求正文中,NameValuePair 对我没有帮助。
Does anybody knows an workaround or a solution?
有人知道解决方法或解决方案吗?
采纳答案by Bozho
The javadoc says:
javadoc 说:
Deprecated. use setRequestEntity(RequestEntity)
已弃用。使用 setRequestEntity(RequestEntity)
RequestEntity has a lot of implementors, namely:
RequestEntity 有很多实现者,即:
ByteArrayRequestEntity, FileRequestEntity, InputStreamRequestEntity, MultipartRequestEntity, StringRequestEntity
ByteArrayRequestEntity、FileRequestEntity、InputStreamRequestEntity、MultipartRequestEntity、StringRequestEntity
Use the one that suits you:
使用适合您的一种:
- if your xml is in a
String
, use theStringRequestEntity
- if it is in a file, use the
FileRequestEntity
- 如果您的 xml 在 a 中
String
,请使用StringRequestEntity
- 如果它在文件中,请使用
FileRequestEntity
and so on.
等等。
回答by Tony Schwartz
Yes, so for example,
是的,例如,
post.setRequestEntity( new StringRequestEntity( xml ) );
instead of
代替
post.setRequestBody( xml );