How to Post an XML in HTTP
<p>I need to send an xml content via http. I had already tried some methods but it doesn't works out. The parameters should look like this {"action"=>"login", "controller"=>"session", "user"=>{"username"=>"123@gmail.com", "password"=>"[FILTERED]"}} whereas that action and controller parameter are predefined in server (i.e) inside the user tag the both username and password should come...</p>

<p>The XML code block will be</p>

<pre><code>             &lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;
             &lt;user&gt;
             &lt;username&gt;xxx&lt;/username&gt;
             &lt;password&gt;yyyy&lt;/password&gt;
             &lt;/user&gt;
</code></pre>

<p>my code is</p>

<pre><code>String param1 = null;
param1 = URLEncoder.encode(charset);

String param2 = null;
try {
    param2 = URLEncoder.encode("x@yyy.com", charset);
} catch (UnsupportedEncodingException e3) {
    // TODO Auto-generated catch block
    e3.printStackTrace();
} 
String param3 = null;
try {
    param2 = URLEncoder.encode("xxx", charset);
} catch (UnsupportedEncodingException e4) {
    // TODO Auto-generated catch block
    e4.printStackTrace();
}

String query = String.format("user=&amp;username=%s&amp;password=%s", param1, param2,param3);*/

URLConnection urlConnection = null;
try {
    urlConnection = new URL(url).openConnection();
} catch (MalformedURLException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
} catch (IOException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
}
urlConnection.setUseCaches(false);
urlConnection.setDoOutput(true); // Triggers POST.
//urlConnection.setRequestProperty("accept-charset", charset);
urlConnection.setRequestProperty("content-type", "application/xml");

OutputStreamWriter writer = null;
try {
    writer = new OutputStreamWriter(urlConnection.getOutputStream());
    writer.write(query); // Write POST query string (if any needed).
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} finally {
    if (writer != null) try { writer.close(); } catch (IOException logOrIgnore) {}
}

try {
    InputStream result = urlConnection.getInputStream();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
</code></pre>
