Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostMethod setRequestBody(String) deprecated - why?

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?

like image 315
Trick Avatar asked Jan 19 '10 09:01

Trick


2 Answers

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.

like image 70
Bozho Avatar answered Sep 19 '22 18:09

Bozho


Yes, so for example,

post.setRequestEntity( new StringRequestEntity( xml ) ); 

instead of

post.setRequestBody( xml ); 
like image 20
Tony Schwartz Avatar answered Sep 20 '22 18:09

Tony Schwartz