(七)发送、接收SOAP消息(以HttpClient方式)(2)
一.为什么要用soap
原本我们使用web服务都是根据wsdl生成客户端(生成一堆java文件)然后再调用,本章节讲解如何用soap消息来替代这种方式。
二、SOAP消息格式

- SOAP(简单对象访问协议)是基于XML的消息格式。下面是一个简单的SOAP消息:
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Header>
</soap:Header> <soap:Body> ... message data ... <soap:Fault>
</soap:Fault> </soap:Body> </soap:Envelope>
正如你可以看到一个SOAP消息包括:
- Envelope
- Header
- Body
- Message Data
- Fault (optional)
相同的SOAP消息结构用于客户端和Web Service服务器之间的请求和响应。
Body内的Fault元素是可选的。只有Web服务中发生内部错误里才返回。否则,返回正常信息数据。
SOAP不指定一个消息从客户端如何获取到Web Service,但最常见的情况是通过HTTP。
三、案例
3.1 发布服务
- 服务接口
package service; import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; @WebService
public interface IFirstService { @WebResult(name = "addResult")
public int add(@WebParam(name = "x") int x, @WebParam(name = "y") int y);
}
- 服务接口实现类
package service; import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; @WebService(endpointInterface = "service.IFirstService")
public class IFirstServiceImpl implements IFirstService { @Override
public int add(int x, int y) { return x + y;
} }
- 发布服务
package publish;
import javax.xml.ws.Endpoint;
import service.IFirstServiceImpl;
public class TestPublish {
public static void main(String[] args) {
Endpoint.publish("http://localhost:3030/first", new IFirstServiceImpl());
System.out.println("发布成功.....");
}
}
- 查看wsdl文件
<definitions targetNamespace="http://service/" name="IFirstServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://service/" schemaLocation="http://localhost:3030/first?xsd=1" />
</xsd:schema>
</types>
<message name="add">
<part name="parameters" element="tns:add" />
</message>
<message name="addResponse">
<part name="parameters" element="tns:addResponse" />
</message>
<portType name="IFirstService">
<operation name="add">
<input wsam:Action="http://service/IFirstService/addRequest"
message="tns:add" />
<output wsam:Action="http://service/IFirstService/addResponse"
message="tns:addResponse" />
</operation>
</portType>
<binding name="IFirstServiceImplPortBinding" type="tns:IFirstService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document" />
<operation name="add">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="IFirstServiceImplService">
<port name="IFirstServiceImplPort" binding="tns:IFirstServiceImplPortBinding">
<soap:address location="http://localhost:3030/first" />
</port>
</service>
</definitions>
http://localhost:3030/first?xsd=1文件
<xs:schema version="1.0" targetNamespace="http://service/">
<xs:element name="add" type="tns:add" />
<xs:element name="addResponse" type="tns:addResponse" />
<xs:complexType name="add">
<xs:sequence>
<xs:element name="x" type="xs:int" />
<xs:element name="y" type="xs:int" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="addResponse">
<xs:sequence>
<xs:element name="addResult" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:schema>
二、客户端发送、接收soap消息
package com.shyroke.webService_client; import java.io.IOException;
import java.io.StringReader; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; /**
* Hello world!
*
*/
public class App {
public static void main(String[] args) { try { /**
* 定义发送soap的消息
*/
StringBuffer str_xml = new StringBuffer();
str_xml.append(
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service/\">");
str_xml.append(" <soapenv:Header/>");
str_xml.append(" <soapenv:Body>");
str_xml.append("<ser:add>");
str_xml.append(" <x>11</x>");
str_xml.append("<y>20</y>");
str_xml.append("</ser:add>");
str_xml.append("</soapenv:Body>");
str_xml.append("</soapenv:Envelope>"); /**
* 定义post请求
*/
// 定义post请求地址
HttpPost httpPost = new HttpPost("http://localhost:3030/first?wsdl");
// 定义post请求的实体
HttpEntity entity = new StringEntity(str_xml.toString());
// 设置post请求的实体和头部
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "text/xml; charset=UTF-8"); /**
* 发送请求并获取返回数据
*/
@SuppressWarnings("deprecation")
DefaultHttpClient client = new DefaultHttpClient();
// 发送请求并获取返回数据
HttpResponse response = client.execute(httpPost);
// 获取返回数据中的实体
HttpEntity respon_entity = response.getEntity();
// 将返回数据的实体转为字符串
String respon_str = EntityUtils.toString(respon_entity); // 解析字符串
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(respon_str));
Element rootElement = document.getRootElement(); Element resultElement = rootElement.element("Body").element("addResponse").element("addResult");
String addResult = resultElement.getText();
System.out.println("addResult = " + addResult); } catch (Exception e) {
e.printStackTrace();
} }
}
结果:

(七)发送、接收SOAP消息(以HttpClient方式)(2)的更多相关文章
- 2.技巧: 用 JAXM 发送和接收 SOAP 消息—Java API 使许多手工生成和发送消息方面必需的步骤自动化
转自:https://www.cnblogs.com/chenying99/archive/2013/05/23/3094128.html 技巧: 用 JAXM 发送和接收 SOAP 消息—Java ...
- (六)发送、接收SOAP消息(1)
一.为什么要用soap 原本我们使用web服务都是根据wsdl生成客户端(生成一堆java文件)然后再调用,本章节讲解如何用soap消息来替代这种方式. 二.SOAP消息格式 SOAP(简单对象访问协 ...
- webservice系统学习笔记5-手动构建/发送/解析SOAP消息
手动拼接SOAP消息调用webservice SOAP消息的组成: 1.创建需要发送的SOAP消息的XML(add方法为例子) /** * 创建访问add方法的SOAP消息的xml */ @Test ...
- Spring使用MappingJackson2MessageConverter发送接收ActiveMQ消息
一.Spring使用JmsTemplate简化对JMS的访问 在JAVA对JMS队列访问中,使用默认的JMS支持将存在大量的检查型异常.通过Spring的支持,可以将所有的JMS的检查型异常转换为运行 ...
- Wpf发送接收 win32消息
#region WPF发送和接收win32消息 public const int WM_GETTEXT = 0x0D; public const int WM_SETTEXT = 0x0C; publ ...
- java通过HttpClient方式和HttpURLConnection方式调用WebService接口
1.引入maven依赖: <dependency> <groupId>org.apache.httpcomponents</groupId> <artifac ...
- webservice05#soap消息
1, SOAPMessage结构图 2, SOAP消息的创建 1>前面的一个简单WebService 服务 package com.yangw.soap.service; import jav ...
- [3] MQTT,mosquitto,Eclipse Paho---怎样使用 Eclipse Paho MQTT工具来发送订阅MQTT消息?
在上两节,笔者主要介绍了 MQTT,mosquitto,Eclipse Paho的基本概念已经怎样安装mosquitto. 在这个章节我们就来看看怎样用 Eclipse Paho MQTT工具来发送接 ...
- 如何在WCF中用TcpTrace工具查看发送和接收的SOAP消息
WCF对消息加密(只对消息加密,不考虑Authorize)其实很简单,只要在server和client端的binding加入security mode为Message(还有Transport, Tra ...
随机推荐
- 使用多个tomcat如何修改端口号
一.找到tomcat下conf文件夹下server.xml: 二.修改8080端口 三.修改8009端口 四.修改8005端口 修改后同时启动多个tomcat成功.
- 开发WINDOWS服务程序
开发WINDOWS服务程序 开发步骤: 1.New->Other->Service Application 2.现在一个服务程序的框架已经搭起来了,打开Service1窗口,有几个属性说明 ...
- [Java复习] MQ
1. 为什么要用MQ? 解耦,异步,削峰 2. MQ的优点和缺点? 优点: 解耦.异步.削峰 缺点: 1. 系统可用性降低. 外部依赖越多,越容易挂.如果MQ挂了,怎么处理? 2. 系统复杂度提高. ...
- 阶段5 3.微服务项目【学成在线】_day18 用户授权_18-微服务之间认证-需求分析
4.1 需求分析 前边章节已经实现了用户携带身份令牌和JWT令牌访问微服务,微服务获取jwt并完成授权. 当微服务访问微服务,此时如果没有携带JWT则微服务会在授权时报错. 测试课程预览: 1.将课程 ...
- jmeter -- beanshell 执行本地py文件
Process proc = Runtime.getRuntime().exec("python /Users/lucax/Desktop/工作/Ai双师项目/性能优化迭代_脚本准备/获取学 ...
- 一秒 解决 ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql 问题
提示:ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'.前两天也出现过这个问题,网上找了一个比 ...
- AES加密(java和C#)
需求:Java和C#进行数据交互,互相采用AES/CBC/PKCS5Padding进行加解密 Java加密和解密的代码如下: /** * 加密 1.构造密钥生成器 2.根据 ecnodeRules 规 ...
- (十六)toString()的用法
每一个非基本类型的对象都有一个toString()方法,而且当编译器需要一个String而你却只有一个对象时候,该方法便会被调用. public class te { public String to ...
- WPF引入OCX控件
(方法一) https://www.cnblogs.com/guaniu/archive/2013/04/07/3006445.html (方法二) 1.先注册OCX控件:(有的把OCX 控件封装到E ...
- PYTHON指定国内PIP源
一.LINUX: vi ~/.pip/pip.conf 输入内容: [global]index-url = http://pypi.douban.com/simple/[install]trusted ...