0

I need to send an xml request to a web service and accept xml response from that web service.

Below is the Code:

public static void main(String[] args) throws IOException {

        String url ="http://XX.XXX.X.XX:80/test";
        String urlParameters = "";
        StringBuilder test = new StringBuilder(768);

        test.append("<?xml version='1.0'?>\n"+
    "<!DOCTYPE COMMAND PUBLIC '-//Ocam//DTD XML Command 1.0//EN' 'xml/command.dtd'>\n"+
                "<COMMAND>\n"+

                "<TYPE>EXUSRBALREQ</TYPE>\n"+
                "<DATE>14-03-17</DATE>\n"+
"<EXTNWCODE>MD</EXTNWCODE>\n"+
"<MSISDN>57625960</MSISDN>\n"+
"<PIN>47565</PIN>\n"+
"<LOGINID></LOGINID>\n"+
"<PASSWORD></PASSWORD>\n"+
"<EXTCODE>AD10001</EXTCODE>\n"+
"<EXTREFNUM>12345</EXTREFNUM>\n"+               
                "</COMMAND>\n");

        System.out.println(test);

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content_Type", "application/xml");          
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("Sending post on the URL"+url);      
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream(),"UTF-8"));
        String inputLine;
        StringBuffer response = new StringBuffer();     
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }           
        in.close();

        System.out.println(responseCode);
        System.out.println(response);


    }

Whenever i run the application i am getting 200 status however below is the response which i get

Response:

Sending post on the URLhttp://XXX.XXX.X.XX:80/test
200
mclass^2&pid^61:6002:Your request is invalid.  Please call 8900

Can Somebody help me with this.

Regards,

Amit Gupta

amg_amit
  • 485
  • 1
  • 3
  • 9
  • You should ask the provider of the webservice. It looks like your request got through, but it is incorrect. – NeplatnyUdaj Mar 15 '17 at 11:25
  • Thanks Neplatny for the reply , i did check with the provider however they have mentioned that the xml request is not reaching then hence they are sending that response, is there any way where i can trace the message which is going, currently i am performing this test on windows machine. – amg_amit Mar 15 '17 at 11:27
  • There is a typo in your code: The HTTP header is "Content-Type", not "Content_Type". That may be the problem. – sleske Mar 15 '17 at 11:28
  • This doesn't make any sense: "request is not reaching then hence they are sending that response". How can they send response when they don't receive a request? Anyway you can check what's going on using wireshark. – NeplatnyUdaj Mar 15 '17 at 11:28
  • Hi Neplanty, I meant that they are not receiving the request in a proper format, i need to trace the request so that i can be sure that correct format is going – amg_amit Mar 15 '17 at 11:30
  • Ah, ok. Anyway someone already posted an answer. To check what' going on, use wireshark. It's the essential tool when debugging protocols. – NeplatnyUdaj Mar 15 '17 at 11:33
  • Perhaps their web service is SOAP based, in which case, simple XML isn't going to work. – dsp_user Mar 15 '17 at 11:34

1 Answers1

1

Your code assembles the XML to send in the StringBuilder called test.

However, you never actually send the contents of test, so it's not surprising the server does not receive the XML. Please fix that.

See for example Sending HTTP POST Request In Java for how to send a POST request in Java.

Community
  • 1
  • 1
sleske
  • 73,934
  • 32
  • 166
  • 212
  • I rectified the Content-type as @sleske mentioned and i am getting response – amg_amit Mar 15 '17 at 11:52
  • however that is not an expected response, after going through wireshark as @Neplatny mentioned it looks like the request is not being passed, need to add the and then retry – amg_amit Mar 15 '17 at 11:52
  • Added wr.write(test.getBytes()) , changed the request from Stringbuilder to String and when checked in wireshark the request is passing. – amg_amit Mar 15 '17 at 13:50