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

7
  • You should ask the provider of the webservice. It looks like your request got through, but it is incorrect. Commented Mar 15, 2017 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. Commented Mar 15, 2017 at 11:27
  • There is a typo in your code: The HTTP header is "Content-Type", not "Content_Type". That may be the problem. Commented Mar 15, 2017 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. Commented Mar 15, 2017 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 Commented Mar 15, 2017 at 11:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I rectified the Content-type as @sleske mentioned and i am getting response
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
Added wr.write(test.getBytes()) , changed the request from Stringbuilder to String and when checked in wireshark the request is passing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.