Post发送SOAP格式内容访问WebService接口

需要jar包:

commons-logging-1.0.4.jar
httpclient-4.3.2.jar
httpcore-4.3.2.jar

测试:

package http;

public class TestSoap {
    public static void main(String[] args) {
        String soapXml="<?xml version=\"1.0\" encoding=\"utf-16\"?>" +
                "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值