如何从webService响应中获取XML响应?

时间:2016-09-29 11:28:20

标签: java xml web-services soap

我必须创建一个将webService响应作为xml获取的方法。我知道如何使用Java类创建,但问题是从webService获得xml响应。

这些网络服务是基于肥皂的。

提前致谢。

2 个答案:

答案 0 :(得分:1)

我刚刚解决了我的问题。 HttpURLConnection帮助我。

以下代码块显示了如何在xml中制作java响应的海报,例如Mozilla Poster。

public static void main(String[] args) {
    try {
        String uri = "http://test.com/IntegratedServices/IntegratedServices.asmx?op=GetUserInfo";

        String postData = new XmlTest().xmlRequest("QWERTY10");

        URL url = new URL(uri);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true); // This is important. If you not set doOutput it is default value is false and throws java.net.ProtocolException: cannot write to a URLConnection exception 
        connection.setRequestMethod("POST"); // This is method type. If you are using GET method you can pass by url. If method post you must write
        connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); // it is important if you post utf-8 characters

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); // This three lines is importy for POST method. I wrote preceding comment.
        wr.write(postData.getBytes());
        wr.close();

        InputStream xml = connection.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(xml));
        String line = "";
        String xmlResponse = "";
        while ((line = reader.readLine()) != null) {
            xmlResponse += line;
        }

        File file = new File("D://test.xml"); // If you want to write as file to local.
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(xmlResponse);
        fileWriter.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public String xmlRequest(String pin) {
    return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
            + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
            + "  <soap:Body>\n"
            + "    <GetUserInfo xmlns=\"http://tempuri.org/\">\n"
            + "      <pin>" + pin + "</pin>\n"
            + "    </GetUserInfo>\n"
            + "  </soap:Body>\n"
            + "</soap:Envelope>";
}

我希望这有助于谁想要xml作为回应。我还给我的代码写了详细的评论。

答案 1 :(得分:0)

对于肥皂类型的webservice:

Parsing SOAP Response in Java

如需休息,请查看以下链接:

http://duckranger.com/2011/06/jaxb-without-a-schema/