I am trying to write a servlet that will send a XML file (xml formatted string) to another servlet via a POST. (Non essential xml generating code replaced with ‘Hello there’)
StringBuilder sb= new StringBuilder(); sb.append('Hello there'); URL url = new URL('theservlet's URL'); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod('POST'); connection.setRequestProperty('Content-Length', '' + sb.length()); OutputStreamWriter outputWriter = new OutputStreamWriter(connection.getOutputStream()); outputWriter.write(sb.toString()); outputWriter.flush(); outputWriter.close();
This is causing a server error, and the second servlet is never invoked.
This kind of thing is much easier using a library like HttpClient. There’s even a post XML code example: