Http client Post xml file in java

11,442

Below is a sample POST operation:

URL url = new URL("http://14.140.66.142:80/MSMQ/private$/votes");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");

OutputStream os = connection.getOutputStream();

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
FileReader fileReader = new FileReader("C:\\Documents and Settings\\Nagra\\My Documents\\Responseserver\\workingVoting\\VoteSubmitter\\Body.xml");
StreamSource source = new StreamSource(fileReader);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);

os.flush();
connection.getResponseCode();
connection.disconnect();
Share:
11,442
bharathi
Author by

bharathi

Updated on June 04, 2022

Comments

  • bharathi
    bharathi almost 2 years

    I need to send a xml file to the following link\

        http://14.140.66.142:80/MSMQ/private$/votes
    

    This is my code.

       URL url = new URL("http://14.140.66.142:80/MSMQ/private$/votes");
        URLConnection con = url.openConnection();
        String document = "C:\\Documents and Settings\\Nagra\\My Documents\\Responseserver\\workingVoting\\VoteSubmitter\\Body.xml";
    
        FileReader fr = new FileReader(document);
        // specify that we will send output and accept input
        con.setDoInput(true);
        con.setDoOutput(true);
        char[] buffer = new char[1024*10];
    
        int b_read = 0;
    
        if ((b_read = fr.read(buffer)) != -1)
    
        {
            con.setRequestHeader ( "Content-Type", "text/xml" );
            con.setRequestProperty("SOAPAction","MSMQMessage");
            con.setRequestProperty("Proxy-Accept","NonInteractiveClient" );
            con.setRequestProperty("CONNECTION", "close");
            con.setRequestProperty("CACHE-CONTROL", "no-cache");
            con.setRequestProperty("USER-AGENT", "OpenTV-iAdsResponder_1_0");
            OutputStreamWriter writer = new OutputStreamWriter( con.getOutputStream() );
            writer.write(buffer, 0, b_read);
            PrintWriter pw = new PrintWriter(con.getOutputStream());
            pw.write(buffer, 0, b_read);
           pw.close();
            System.out.println("written");
    
    
      }
      catch( Throwable t )
    {
        t.printStackTrace( System.out );
    }
    
      }
      }
    

    I don't Know whether it is right code.If i run this code I am not able to receive the xml file on the server side.Can anyone help me where i gone wrong in my code.

  • bharathi
    bharathi over 12 years
    This is the error which i Got while running your code. org.xml.sax.SAXParseException: Content is not allowed in prolog. at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser‌​.parse(AbstractSAXPa‌​rser.java:1231) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl‌​.transformIdentity(T‌​ransformerImpl.java:‌​609) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl‌​.transform(Transform‌​erImpl.java:707) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl‌​.transform(Transform‌​erImpl.java:313) at hhtprequest.main(hhtprequest.java:52)
  • bdoughan
    bdoughan over 12 years
    @bharathi - Can you try the transform outside the HTTP operation (set the output to System.out)? This code has been successful for others:stackoverflow.com/questions/6665769/…