title: Webservice call returns error 500
body: <p>I have started a small project in Java.<br>
  I have to create a client which will send xml to a url as a HTTP POST request.<br>
  I try it using <code>java.net.*</code>  package (Following is the piece of code) but I am getting error as follows:</p>
  
  <pre><code>java.io.IOException: Server returned HTTP response code: 500 for URL: "target url"
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
      at newExample.main(newExample.java:36)
  </code></pre>
  
  <p>My code is as follows:</p>
  
  <pre><code>try {
          URL url = new URL("target url");
  
          URLConnection connection = url.openConnection();
  
          if( connection instanceof HttpURLConnection )
              ((HttpURLConnection)connection).setRequestMethod("POST");
  
          connection.setRequestProperty("Content-Length", Integer.toString(requestXml.length()) );
          connection.setRequestProperty("Content-Type","text/xml; charset:ISO-8859-1;");
          connection.setDoOutput(true);
          connection.connect();           
  
          // Create a writer to the url
          PrintWriter writer = new PrintWriter(new
          OutputStreamWriter(connection.getOutputStream()));
  
          // Get a reader from the url
          BufferedReader reader = new BufferedReader(new
          InputStreamReader(connection.getInputStream()));
  
          writer.println();
          writer.println(requestXml);
          writer.println();
          writer.flush();
  
          String line = reader.readLine();
              while( line != null ) {
                      System.out.println( line );
                      line = reader.readLine();
              }
  
  
      } catch (MalformedURLException e) {
                      e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  </code></pre>
  
  <p>Please help with suitable examples or any other ways of doing this.</p>
  
  <p>Point errors/mistakes in above code or other possibilities.</p>
  
  <p>My Web Service is in spring framework</p>
  
  <p>xml to send is in the string format: requestXml</p>