title: Looking for java equivalent for sending xml string via window.XMLHttpRequest
body: <p>In JavaScript, we have the following implementation:</p>
  
  <pre><code>function sendRMSRequest(xmlString, url){
  
      if(window.XMLHttpRequest){
          xmlRequest = new XMLHttpRequest();
      }
  
      xmlRequest.open("POST", url, false);
      xmlRequest.onreadystatechange = function() {processRequest();};
      xmlRequest.send(xmlString);
  }
  </code></pre>
  
  <p>xmlString has the following String:</p>
  
  <pre><code>&lt;TestRequest xmlns='http://???.???.????' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'&gt;&lt;/TestRequest&gt;
  </code></pre>
  
  <p>I want to move this functionality to the server side and I can't figure out how to implement this functionality in java.</p>
  
  <p>I currently have the following java code:</p>
  
  <pre><code>public static void main(String[] args) throws IOException {
      String endPoint = "http://????.????/service";
  
  
      String xml =  "&lt;TestRequest xmlns='http://???.???.????' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'&gt;&lt;/TestRequest&gt;";
  
      System.out.println(xml);
  
      URL url = new URL(endPoint);
      HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
      httpCon.setDoInput(true);
      httpCon.setDoOutput(true);
      Authenticator.setDefault(new MyAuthenticator());
  
      httpCon.setReadTimeout(0);
      httpCon.setConnectTimeout(0);
      httpCon.setRequestMethod("POST");
      httpCon.setRequestProperty("Content-Type", "text/plain"); 
      httpCon.setRequestProperty("charset", "utf-8");
      httpCon.setRequestProperty("", xml);
  
  
      PrintWriter pw = new PrintWriter(httpCon.getOutputStream());  
  
      pw.write(xml);
      pw.flush();
      pw.close();  
  
      System.out.println(httpCon.getResponseCode());
      System.out.println(httpCon.getResponseMessage());
      System.out.println(httpCon.getContentType());
  
  
  
      String returnString = null;
      StringBuffer sb = null;
      BufferedInputStream in;
  
      // set up httpConn code not included same as previous
      in = new BufferedInputStream(httpCon.getInputStream());  
  
      int x = 0;
  
      sb = new StringBuffer();
  
      while ((x = in.read()) != -1) {
          sb.append((char) x);
      }
  
      in.close();
      in = null;
  
      if (httpCon != null) {
          httpCon.disconnect();
      }
  
      returnString = sb.toString();   
      System.out.println("Return String = " + returnString);
  
  }
  </code></pre>
  
  <p>I get the following error when running the code above:</p>
  
  <pre><code>org.xml.sax.SAXParseException: Premature end of file.
  </code></pre>
  
  <p>Any help would be appreciated.</p>
  
  <p>Thanks
  Daniel</p>