Java使用SOAP调用WebService

1、webservice三要素
Webservice三要素包括:SOAP、WSDL、UDDI
soap用来描述传递信息的格式;
wsdl用来描述如何访问具体的接口
uudi用来管理,分发,查询webservice。
soap使用基于XML的数据结构和超文本传输协议(HTTP)的组合定义了一个标准的方法来使用INternet上各种不同操作环境中的分布式对象;
2、相关定义:
1、基于类对象的传输协议
2、soap封装(envlop),定义了一个框架,描述消息中的内容是什么,是谁发送的,
应当接受并处理它以及如何处理他们;
3、soap编码规则(encoding rules),定义了一种序列化机制,用于标识应用程序需要使用的数据类型的实例;
4、soap rpc 表示,它定义了一个协定,用于标识远程过程中调用和应答;
5、soap 绑定(binding),定义了soap使用哪种协议交换性喜,使用HTTP/TCP/UDP协议都可以;
3、构建模块
一条 SOAP 消息就是一个普通的 XML 文档,包含下列元素:
必需的 Envelope 元素,可把此 XML 文档标识为一条 SOAP 消息
可选的 Header 元素,包含头部信息
必需的 Body 元素,包含所有的调用和响应信息
可选的 Fault 元素,提供有关在处理此消息所发生错误的信息
4、语法规则
SOAP 消息必须用 XML 来编码
SOAP 消息必须使用 SOAP Envelope 命名空间
SOAP 消息必须使用 SOAP Encoding 命名空间
SOAP 消息不能包含 DTD 引用
SOAP 消息不能包含 XML 处理指令
5、优点:
1、可扩展的
2、简单的
3、万泉河厂商无关
4、与编程语言无关
5、与平台无关

6、功能实现
1、定义访问地址,以及调用的方法名称

 //地址
    String urlString = "http://124.114.156.246:18010/WmsJcspzl.asmx?wsdl";
    //方法
    String soapActionString = "http://ws.jcspzl/ReceiveJcspzl";

2、连接请求路径,拼接请求体

 URL url = new URL(urlString);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    //拼接请求体
	String soap = ""
			+"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
			+"<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/\">"
			+"<soap:Body>"
			+"<ReceiveJcspzl xmlns=\"http://ws.jcspzl\">"
			+"<I_REQUEST>"
			 +"  </I_REQUEST>"
	        +" </ReceiveJcspzl>"
	        +"  </soap:Body>"
	        +"</soap:Envelope>";

3、设置请求头参数(重点)

 byte[] buf = soap.getBytes();
    //设置一些头参数
    httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
    httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    httpConn.setRequestProperty("SOAPAction", soapActionString);
    httpConn.setRequestMethod("POST");

4、打印响应结果,并进行处理

//输入参数和输出结果
    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);
    OutputStream out = httpConn.getOutputStream();
    out.write(buf);
    out.close();
    
    byte[] datas = readInputStream(httpConn.getInputStream());
    String result = new String(datas);
    String a=result.replaceAll("&gt;", ">").replaceAll("&lt;", "<");
   return a;

5、结果字符串进行xml格式转换

<?xml version="1.0" encoding="utf-8"?>
<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/XMLSchema">
<soap:Body>
    <ReceiveJcspzlResponse
        xmlns="http://ws.jcspzl">
        <ReceiveJcspzlResult>
            <?xml version="1.0" encoding="utf-8"?>
            <DATA>
                <RETDATA>
                    <DANJ_NO>20190824000147</DANJ_NO>
                    <HANGHAO></HANGHAO>
                    <ZFLAG>S</ZFLAG>
                    <ZMESSAGE></ZMESSAGE>
                </RETDATA>
            </DATA>
        </ReceiveJcspzlResult>
    </ReceiveJcspzlResponse>
</soap:Body>
</soap:Envelope>
  • 1
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java使用SOAP客户端调用Web服务并传递对象参数,需要遵循以下步骤: 1. 通过wsimport工具生成客户端代码。 2. 创建包含对象属性的类,该类必须是可序列化的,即需要实现Serializable接口。 3. 创建SOAP消息,并将对象参数添加到SOAP Body中。可以使用JAXB将对象序列化为XML。 4. 发送SOAP消息到Web服务,并解析响应消息。 示例代码: 1. 通过wsimport工具生成客户端代码。 首先需要使用wsimport工具根据WSDL文件生成客户端代码。在命令行中执行以下命令: ``` wsimport -keep http://localhost:8080/WebService?wsdl ``` 其中,-keep选项表示生成的Java文件保存在当前目录中。 2. 创建包含对象属性的类。 假设需要传递的对象是Person,具有姓名和年龄两个属性: ``` public class Person implements Serializable { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ``` 3. 创建SOAP消息。 使用JAXB将Person对象序列化为XML,并添加到SOAP消息的Body中: ``` // 创建JAXB上下文 JAXBContext jaxbContext = JAXBContext.newInstance(Person.class); Marshaller marshaller = jaxbContext.createMarshaller(); // 序列化Person对象为XML Person person = new Person(); person.setName("张三"); person.setAge(20); StringWriter writer = new StringWriter(); marshaller.marshal(person, writer); String xml = writer.toString(); // 创建SOAP消息 MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody body = envelope.getBody(); body.addDocument(new ByteArrayInputStream(xml.getBytes())); ``` 4. 发送SOAP消息到Web服务。 使用生成的客户端代码创建WebService对象,并调用对应的方法: ``` // 创建WebService对象 URL url = new URL("http://localhost:8080/WebService"); WebService service = new WebService(url); // 调用WebService方法 SOAPMessage response = service.call(soapMessage); ``` 其中,WebService使用wsimport生成的客户端代码中的类。call方法用于发送SOAP消息,并返回响应消息。 5. 解析响应消息。 从响应消息的Body中获取返回的数据: ``` SOAPBody responseBody = response.getSOAPBody(); Node responseNode = responseBody.getFirstChild(); String responseXml = responseNode.getTextContent(); // 反序列化XML为对象 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); StringReader reader = new StringReader(responseXml); Person result = (Person) unmarshaller.unmarshal(reader); ``` 完整的代码示例: ``` public class SoapClient { public static void main(String[] args) throws Exception { // 创建JAXB上下文 JAXBContext jaxbContext = JAXBContext.newInstance(Person.class); Marshaller marshaller = jaxbContext.createMarshaller(); // 序列化Person对象为XML Person person = new Person(); person.setName("张三"); person.setAge(20); StringWriter writer = new StringWriter(); marshaller.marshal(person, writer); String xml = writer.toString(); // 创建SOAP消息 MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody body = envelope.getBody(); body.addDocument(new ByteArrayInputStream(xml.getBytes())); // 创建WebService对象 URL url = new URL("http://localhost:8080/WebService"); WebService service = new WebService(url); // 调用WebService方法 SOAPMessage response = service.call(soapMessage); // 解析响应消息 SOAPBody responseBody = response.getSOAPBody(); Node responseNode = responseBody.getFirstChild(); String responseXml = responseNode.getTextContent(); // 反序列化XML为对象 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); StringReader reader = new StringReader(responseXml); Person result = (Person) unmarshaller.unmarshal(reader); System.out.println(result.getName()); System.out.println(result.getAge()); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值