【问题标题】:How to get response as XML from webService response?如何从 webService 响应中获取 XML 响应?
【发布时间】:2016-09-29 11:28:19
【问题描述】:

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

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

提前致谢。

【问题讨论】:

  • 请分享您到目前为止所尝试的内容。我们很乐意为您提供帮助。
  • @Reddy 感谢您的评论。我想在 java 中创建一个类似 Mozilla Poster 的方法。

标签: java xml web-services soap


【解决方案1】:

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

以下代码块显示了我如何制作海报,以便在 java 中获得 xml 响应,如 Mozilla 海报。

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 作为回复的人。我还为我的代码写了详细的注释。

【讨论】:

    【解决方案2】:

    对于soap类型的webservice:

    Parsing SOAP Response in Java

    其他看这个链接:

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

    【讨论】:

    • 我的问题不是解析。我问我怎样才能得到 XML 的响应。我作为答案发布。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    相关资源
    最近更新 更多