How can i create HTTP Request using POST method?

How can i create HTTP request using post method for sending parameter

cpid, pwd, and msg in xml format :

<mms>

<subject>message subject</subject>

<url_image>http://image_url</url_image>

<url_sound>http://sound_url</url_sound>

<url_video>http://video_url</url_video>

<text>message text</text>

<msisdn_sender>6281XYYYYYY</msisdn_sender>

<msisdn_receipient>6281XYYYYYY</msisdn_receipient>

<sid>to be define later</sid>

<trx_id>Unique number</trx_id>

<trx_date>yyyyMMddHHmmss</trx_date>

<contentid>see note</contentid>

</mms>

anyone can help me?

[782 byte] By [kalinggaa] at [2007-10-3 1:34:56]
# 1

Use a URLConnection. Something like this:

URL url = new URL("http://www.somewhere.com/servlet/DoSomething");

URLConnection connection = url.openConnection();

((HttpURLConnection)connection).setRequestMethod("POST");

connection.setDoOutput(true);

connection.setDoInput(true); //Only if you expect to read a response...

connection.setUseCaches(false); //Highly recommended...

connection.setRequestProperty("Content-Type", "text/xml");

PrintWriter output = new PrintWriter(new OutputStreamWriter(connection.getOutputStream()));

output.print("<?xml version=\"1.0\" ?>.......");

output.flush();

output.close();

If you set doInput(true) then you'll have to grab an inputStream (connection.getInputStream()) to read the response from the server.

linxpdaa at 2007-7-14 18:32:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

What if in the post method, i want to send 3 parameters, that is cpid, pwd, & msg. msg is in xml format. Is it meant that i hav to do this :

URL url = new URL("http://www.somewhere.com/servlet/DoSomething");

URLConnection connection = url.openConnection();

((HttpURLConnection)connection).setRequestMethod("POST");

connection.setDoOutput(true);

connection.setDoInput(true); //Only if you expect to read a response...

connection.setUseCaches(false); //Highly recommended...

connection.setRequestProperty("Content-Type", "text/xml");

PrintWriter output = new PrintWriter(new OutputStreamWriter(connection.getOutputStream()));

output.print(cpid, pwd, msg);

output.flush();

output.close();

Or any idea?

kalinggaa at 2007-7-14 18:32:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
& how about the content type? considering that cpid & pwd r string, msg in xml format...?
kalinggaa at 2007-7-14 18:32:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Okay, I originally misunderstood the request. I thought all you were sending was an XML document (like for an AJAX request osr something).

1. To set Parameters use the setRequestProperty or addRequestProperty. For example:

String msg = "<?xml version=\"1.0\" ?><data><property>property1</property><value>Value1</value><property>Property2</property><value>Value2</value></data>";

connection.setRequestProperty(cpid, "123123123");

connection.setRequestProperty(pwd, "123123123");

connection.setRequestProperty(msg, URLEncoder.encode(msg, "UTF-8"));

2. You don't have to set the Content-type in this instance. You're going to pass everything as String objects and handle on the backend.

3. In your servlet be sure to use String msg = URLDecoder.decode(request.getParameter("msg"), "UTF-8"));

to read your original XML.

HTH.

linxpdaa at 2007-7-14 18:32:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Make that:

String msg = "<?xml version=\"1.0\" ?><data><property>property1</property><value>Value1</value><property>Property2</property><value>Value2</value></data>";

connection.setRequestProperty("cpid", "123123123");

connection.setRequestProperty("pwd", "123123123");

connection.setRequestProperty("msg", URLEncoder.encode(msg, "UTF-8"));

linxpdaa at 2007-7-14 18:32:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Please tell me how can i make a HTTP post request in XML.
alokbhayanaa at 2007-7-14 18:32:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...