SOAP JAVA实例
本文档介绍了Java调用SOAP Web Service的简单应用。
一、SOAP
SOAP(Simple Object Access Protocol)是一种交换数据的协议规范,特点是轻量级、基于XML。
请求和响应都是xml的形式,示例如下:
request
POST /regcodeService.asmx HTTP/1.1
Host: weixin.fscut.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "https://weixin.fscut.com/MakeRegCode"
<?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:Header><SessionHeader xmlns="https://weixin.fscut.com"><LangID>int</LangID><SvcVersion>int</SvcVersion><OEMCode>int</OEMCode><SessionID>string</SessionID><OperFrom>string</OperFrom></SessionHeader></soap:Header><soap:Body><MakeRegCode xmlns="https://weixin.fscut.com"><Serial>string</Serial><ExpireDate>string</ExpireDate><Flags>int</Flags><CustomName>string</CustomName><Mobile>string</Mobile><Comment>string</Comment></MakeRegCode></soap:Body>
</soap:Envelope>
response
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?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><MakeRegCodeResponse xmlns="https://weixin.fscut.com"><MakeRegCodeResult><Status>int</Status><ErrMsg>string</ErrMsg><Data>string</Data></MakeRegCodeResult></MakeRegCodeResponse></soap:Body>
</soap:Envelope>
由示例可以看出,一条SOAP消息就是一个XML文档,一般包含以下元素:
Envelope,标识此XML是一条SOAP消息
Header,头部信息
Body,调用和响应信息
Fault,处理消息时发生的错误信息
按照处理XML的方式读写即可。
二、在Java中应用
参考:
How to do a SOAP Web Service call from Java class
首先要创建客户端发起请求,与URL请求类似,步骤如下:
创建连接
设置MimeHeaders
设置参数(soapHeader + soapBody)
解析response
import javax.xml.soap.*;
public class SOAPClientSAAJ {
// SAAJ - SOAP Client Testingpublic static void main(String args[]) {/*The example below requests from the Web Service at:https://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit
To call other WS, change the parameters below, which are:- the SOAP Endpoint URL (that is, where the service is responding from)- the SOAP Action
Also change the contents of the method createSoapEnvelope() in this class. It constructsthe inner part of the SOAP envelope that is actually sent.*/String soapEndpointUrl = "https://www.w3schools.com/xml/tempconvert.asmx";String soapAction = "https://www.w3schools.com/xml/CelsiusToFahrenheit";
callSoapWebService(soapEndpointUrl, soapAction);}
private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {SOAPPart soapPart = soapMessage.getSOAPPart();
String myNamespace = "myNamespace";String myNamespaceURI = "https://www.w3schools.com/xml/";
// SOAP EnvelopeSOAPEnvelope envelope = soapPart.getEnvelope();envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);
/*Constructed SOAP Request Message:<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myNamespace="https://www.w3schools.com/xml/"><SOAP-ENV:Header/><SOAP-ENV:Body><myNamespace:CelsiusToFahrenheit><myNamespace:Celsius>100</myNamespace:Celsius></myNamespace:CelsiusToFahrenheit></SOAP-ENV:Body></SOAP-ENV:Envelope>*/
// SOAP BodySOAPBody soapBody = envelope.getBody();SOAPElement soapBodyElem = soapBody.addChildElement("CelsiusToFahrenheit", myNamespace);SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Celsius", myNamespace);soapBodyElem1.addTextNode("100");}
private static void callSoapWebService(String soapEndpointUrl, String soapAction) {try {// Create SOAP ConnectionSOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP ServerSOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);
// Print the SOAP ResponseSystem.out.println("Response SOAP Message:");soapResponse.writeTo(System.out);System.out.println();
soapConnection.close();} catch (Exception e) {System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");e.printStackTrace();}}
private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {MessageFactory messageFactory = MessageFactory.newInstance();SOAPMessage soapMessage = messageFactory.createMessage();
createSoapEnvelope(soapMessage);
MimeHeaders headers = soapMessage.getMimeHeaders();headers.addHeader("SOAPAction", soapAction);
soapMessage.saveChanges();
/* Print the request message, just for debugging purposes */System.out.println("Request SOAP Message:");soapMessage.writeTo(System.out);System.out.println("\n");
return soapMessage;}
}
除此之外,设置session header的实例如下
SOAPHeader header = soapMessage.getSOAPHeader();
SOAPHeaderElement headerElement = header.addHeaderElement(new QName(SOAP_NAMESPACE_URI,SOAP_KEY_SESSION_HEADER));
SOAPElement langElement = headerElement.addChildElement(SOAP_KEY_LANG_ID);
langElement.addTextNode(SOAP_VALUE_LANG_ID);
response的结果和request类似,可通过解析soapBody的方式获得结果。
处理方式与处理XML类似,不再赘述。
SOAP JAVA实例相关推荐
- java实例方法,Java实例和静态方法
本篇文章帮大家学习java实例和静态方法,包含了java实例和静态方法使用方法.操作技巧.实例演示和注意事项,有一定的学习价值,大家可以用来参考. 类可以有两种类型的方法:实例方法和类方法. 实例方法 ...
- Java-Runoob-高级教程-实例-数组:01. Java 实例 – 数组排序及元素查找
ylbtech-Java-Runoob-高级教程-实例-数组:01. Java 实例 – 数组排序及元素查找 1.返回顶部 1. Java 实例 - 数组排序及元素查找 Java 实例 以下实例演示 ...
- Java-Runoob-高级教程-实例-字符串:13. Java 实例 - 字符串格式化
ylbtech-Java-Runoob-高级教程-实例-字符串:13. Java 实例 - 字符串格式化 1.返回顶部 1. Java 实例 - 字符串格式化 Java 实例 以下实例演示了通过 f ...
- jni java共享变量_Android JNI开发系列(十)JNI访问 Java 实例变量和静态变量
JNI访问 Java 实例变量和静态变量 Java 中的实例变量和静态变量,在本地代码中如何来访问和修改.静态变量也称为类变量(属性),在所有实例对象中共享同一份数据,可以直接通过类名.变量名来访问. ...
- java 方法重载 应用举例,Java 实例 - 重载(overloading)方法中使用 Varargs
以下实例演示了如何在重载方法中使用可变参数:/* author by w3cschool.cc Main.java */public class Main { static void vaTest(i ...
- Java实例开发教程:SpringBoot开发案例
最近在做邮件发送的服务,正常来说SpringBoot整合mail还是很方便的,然而来了新的需求:A请求使用邮箱C发送,B请求使用邮箱D发送,也就是说我们需要配置两套发送服务. 单实例 首先我们来看下单 ...
- java实例变量可以被覆盖吗_Java继承覆盖实例变量
参见英文答案 > Java Inheritance – instance variables overriding 3个 我 ...
- java使用varargs,Java 实例 – Varargs 可变参数使用 - Java 基础教程
Java 实例 Java1.5提供了一个叫varargs的新功能,就是可变长度的参数. "Varargs"是"variable number of arguments&q ...
- Thrift入门及Java实例演示
来源:http://www.micmiu.com/soa/rpc/thrift-sample/ Thrift入门及Java实例演示 作者: Michael日期: 2012 年 6 月 14 日 发表评 ...
最新文章
- 图的最短路径dijkstra算法
- 用apktool批量反编译apk文件
- Java isAlive()和join()的使用
- CSS布局最常用属性float(浮动)和position(定位)
- 战痕————道具系统介绍
- mapperLocations属性通配符的使用
- ubuntu静态IP设置
- php 5.3新增的闭包语法介绍function() use() {}
- 在国外当程序员有多爽
- 天津利用大数据全天候监督财政资金的使用
- keil安装stm32系列
- 第4章 Function语义学
- Centos宝塔面板清理垃圾空间
- .bat脚本初体验——使用批处理bat清洗文件名
- 记得十年前谷歌大量使用python_关于利用Python玩转百万答题
- mysql字段值是什么_什么是数据库字段值
- java 虚拟机JVM
- 海思3518E sample的整体架构venc层源码分析
- 【Windows】Win10设置开机启动项
- WSF操作系统抽象层学习笔记 (五)---事件处理及运行方式
热门文章
- 关于数据库表冗余设计的优缺点
- VHDL——七段译码显示器
- 如何通过售后报修工单系统解决企业售后难题?
- MySQL复制(一):异步复制(Asynchronous replication)
- 在北京住着500多万的房子,却以捡破烂为生是什么样的体验?
- python中continue语句的作用_Pythoncontinue语句有什么作用?详解Pythoncontinue语句的用法...
- IPNC dm368 sd烧写
- 【论文泛读03】卷积LSTM网络:一种短时降雨量预测的机器学习方法
- c语言等号 逗号,C语言逗号运算符和逗号表达式
- mac m1 购买并安装正版我的世界 MC