Java调用WebService接口,SOAP协议HTTP请求返回XML对象

Java调用Web service接口SOAP协议HTTP请求,解析返回的XML字符串:

1. 使用Java的HTTP库发送SOAP请求,并接收返回的响应。

可以使用Java的HttpURLConnection、Apache HttpClient等库。

2. 将返回的响应转换为字符串。

3. 解析XML字符串,可以使用Java的DOM解析器或者其他第三方库,如JDOM、DOM4J等。

4. 解析XML数据,提取需要的信息。

参考代码如下:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class SOAPClient {
    public static void main(String[] args) {
        try {
            // 创建SOAP请求的XML数据
            String soapRequest = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://www.demo.com\">\n"
                    + "   <soapenv:Header/>\n"
                    + "   <soapenv:Body>\n"
                    + "      <web:YourMethodName>\n"
                    + "         <web:Parameter1Name>parameter1Value</web:Parameter1Name>\n"
                    + "      </web:YourMethodName>\n"
                    + "   </soapenv:Body>\n"
                    + "</soapenv:Envelope>";

            // 发送SOAP请求,并接收返回的响应
            String endpoint = "http://localhost:8080/demo_webservice"; // Webservice的URL
            HttpURLConnection connection = (HttpURLConnection) new URL(endpoint).openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            connection.setRequestProperty("SOAPAction", "http://www.demo.com/demoMethodName"); // SOAPAction必须指定
            connection.setDoOutput(true);
            
            OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
            osw.write(soapRequest);
            osw.flush();
            
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            connection.disconnect();
            
            String soapResponse = sb.toString();

            // 解析XML字符串
            DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document document = documentBuilder.parse(soapResponse);
            
            // 提取需要的信息
            // . . .

        } catch (IOException | ParserConfigurationException | SAXException e) {
            e.printStackTrace();
        }
    }
}

通过上述示例代码,使用Java的HTTP库发送SOAP请求,并接收返回的响应,然后将返回的响应

转换为字符串。

接下来,可以使用Java的DOM解析器或其他第三方库解析XML字符串,提取需要的信息

需要将示例代码中的http://localhost:8080/demo_webservice替换为实际的Web service的

URL,并将SOAP请求的XML数据、SOAPAction、以及需要提取的信息进行相应的替换

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 axios 发送 POST 请求来调用 Java WebService SOAP 接口。首先需要了解 SOAP 协议及其请求格式,然后可以按照以下步骤进行调用。 1. 安装 axios 可以使用 npm 安装 axios: ``` npm install axios --save ``` 2. 构造 SOAP 请求 构造 SOAP 请求的格式如下: ```xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.example.com"> <soapenv:Header/> <soapenv:Body> <ser:methodName> <!--Optional:--> <arg0>?</arg0> <!--Optional:--> <arg1>?</arg1> </ser:methodName> </soapenv:Body> </soapenv:Envelope> ``` 其中,`ser:methodName` 为要调用Java WebService 方法名,`arg0`、`arg1` 等为方法参数,可以根据实际情况进行修改。 3. 发送 SOAP 请求 使用 axios 发送 SOAP 请求的代码如下: ```javascript import axios from 'axios' const url = 'http://example.com/soap/service' // WebService 地址 const action = 'http://service.example.com/methodName' // SOAPAction const xml = ` <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.example.com"> <soapenv:Header/> <soapenv:Body> <ser:methodName> <!--Optional:--> <arg0>?</arg0> <!--Optional:--> <arg1>?</arg1> </ser:methodName> </soapenv:Body> </soapenv:Envelope> ` // SOAP 请求体 axios.post(url, xml, { headers: { 'Content-Type': 'text/xml;charset=UTF-8', 'SOAPAction': action } }).then(response => { console.log(response.data) }).catch(error => { console.log(error) }) ``` 其中,`url` 为 WebService 地址,`action` 为 SOAPAction,需要根据实际情况进行修改。`xml` 为 SOAP 请求体,需要根据实际情况进行修改。在发送请求时,需要指定 `Content-Type` 为 `text/xml;charset=UTF-8`,并在请求头中加入 `SOAPAction`。 4. 解析响应 Java WebService 返回的响应也是 SOAP 格式的,需要进行解析。可以使用第三方库 `xml2js` 将响应转换为 JavaScript 对象,代码如下: ```javascript import axios from 'axios' import { parseString } from 'xml2js' const url = 'http://example.com/soap/service' // WebService 地址 const action = 'http://service.example.com/methodName' // SOAPAction const xml = ` <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.example.com"> <soapenv:Header/> <soapenv:Body> <ser:methodName> <!--Optional:--> <arg0>?</arg0> <!--Optional:--> <arg1>?</arg1> </ser:methodName> </soapenv:Body> </soapenv:Envelope> ` // SOAP 请求体 axios.post(url, xml, { headers: { 'Content-Type': 'text/xml;charset=UTF-8', 'SOAPAction': action } }).then(response => { parseString(response.data, (error, result) => { if (error) { console.log(error) } else { console.log(result) } }) }).catch(error => { console.log(error) }) ``` 在响应中,可以通过 `result.Body` 获取到 Java WebService 返回的实际内容,需要根据实际情况进行解析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值