0

I am trying to send a xml file to an URL, and it should respond me with an other xml file. I am trying to do this with the code below :

        URL url = new URL("url.example.com");
        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("path\\test.xml");
        StreamSource source = new StreamSource(fileReader);
        StreamResult result = new StreamResult(os);
        transformer.transform(source, result);
        os.flush();

        connection.getResponseCode();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        try {
            Document docxml = db.parse(connection.getInputStream());
            System.out.println("Input Stream : " + docxml.toString());
        }catch(Exception e) {
            System.out.println("Input Stream : " + e.getMessage());
        }
        connection.disconnect();

When i run the code i'm getting this error :

Input Stream : Server returned HTTP response code: 500 for URL

I am sure the url and the file are good, is there any error in my code and the way i send my http request? Or does the error comes from the server or anything? If you have any idea or suggestion about my code or about what i could do to debug and know where the problem come from, thanks.

Axel
  • 165
  • 3
  • 14

0 Answers0