0

Is there any alternative to pass URL parameters using PostMethod? After this an XML needs to be posted along with the URL. Since it is a post request, the URL parameters should be passed in the Request body and should not be visible. Can addParameter method be used?

URL- http://mytest.com?abc=xyz&token=aisk%2s

1)

//this works ( no utf-8 encoding)
PostMethod pm =new PostMethod("http://mytest.com");
pm.setQueryString("abc=xyz");
pm.setQueryString("token=aisk%2s");

2)

// it encodes utf-8 and fails
PostMethod pm =new PostMethod("http://mytest.com");
NameValuePair [] nvp= new NameValuePair[2];
nvp[0]=new NameValuePair("abc","xyz");
nvp[1]=new NameValuePair("token","aisk%2s");
//encodes the token value as aisk%252s
pm.setQueryString(nvp);

An XML needs to be posted after setting the above URL parameters.

pm.setRequestEntity(new StringRequestEntity(xml, "application/xml", "UTF-8"));
Shrihastha
  • 71
  • 1
  • 5
  • 16

1 Answers1

0

What about using parts instead of adding your parameter to the url?

You can add various datatypes as well:

// using **MultipartEntity**

multipartContent.addPart("user", new StringBody("admin", ContentType.TEXT_PLAIN));
multipartContent.addPart("content", new InputStreamBody(new FileInputStream(file), ContentType.DEFAULT_BINARY));

and of course you can get a page, as the status of your request.

On your server, thread them like they are variables in your $_REQUEST / $_POST array.

use

var_dump($_REQUEST)

to see the content.

Soley
  • 1,716
  • 1
  • 19
  • 33
  • I think you don't need to be worried about the encoding in your url anymore as if you are sending the whole file to the server. Also you can send ByteArrayBody if you want to send bytes. – Soley May 04 '15 at 18:05
  • Is there a way to add XML as a String using MultipartEntity? – Shrihastha May 04 '15 at 18:12
  • You may need to study string to xml and xml to string from java to php and etc: http://php.net/manual/en/domdocument.loadxml.php – Soley May 04 '15 at 18:16
  • Actually, using MultipartEntity how to post an XML string? – Shrihastha May 04 '15 at 18:47
  • Well, as long as you have **new StringBody(yourXML, ContentType.TEXT_PLAIN)** you can send any string to your server. If you are asking how to send XMLDOM object, then check http://www.mkyong.com/tutorials/java-xml-tutorials/ or Jax or any *Java and xml* resources :) – Soley May 04 '15 at 18:59