首页 技术 正文
技术 2022年11月15日
0 收藏 535 点赞 4,499 浏览 2884 个字

soap(简单对象访问协议),它是在http基础之上传递xml格式数据的协议。soap协议分为两个版本,soap1.1和soap1.2。 在学习webservice时我们有一个必备工具叫做tcpmon,该工具可以直接下载得到。使用tcpmon可以嗅探网络中传输的数据,便于我们更好的理解soap协议。

下载好tcpmon之后,打开该软件,如图简单设置

tcpmon相当于一个代理服务器,打开tcpmon后,如果把监听端口设置为9999,目标端口设置为8888,当用户访问9999端口时,消息会被tcpmon监听到,同时tcpmon会把消息转发给目标端口,即8888,服务端返回的数据也是先到达tcpmon,tcpmon再把数据转发给客户端。

通过我们发送的内容那一栏我们可以看到发送的数据,就是那一串xml数据,既然拿到了xml数据,我们就可以使用应用程序发送一个xml字符串,看是否能够调用服务端的数据(服务端的代码WebService学习笔记系列(一)),我们这里只说客户端调用。

public class MyTest2 {    public static void main(String[] args) {
try {
URL url = new URL("http://127.0.0.1:8888/helloService?wsdl");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//get请求以下两个默认即可,post请求就都设置为true
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
con.setRequestMethod("POST");
OutputStream out = con.getOutputStream();
StringBuffer sb = new StringBuffer();
sb.append("<S:Envelope xmlns:S=")
.append("\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body>")
.append("<ns2:getUser xmlns:ns2=\"http://webservice.lenve/\">")
.append("</ns2:getUser></S:Body></S:Envelope>");
out.write(sb.toString().getBytes());
InputStream is = con.getInputStream();
int len = -1;
byte[] bytes = new byte[1024];
StringBuffer buffer = new StringBuffer();
while((len=is.read(bytes))!=-1){
buffer.append(new String(bytes, 0, len));
}
System.out.println(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
}

我们这里通过HttpURLConnection发送一个post请求,请求中携带的字符串就是在tcpmon中嗅探到的那一段xml文本。

输出:

<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:getUserResponse xmlns:ns2="http://webservice.lenve/"><return><nickname>zhangsan</nickname><username>张三</username></return></ns2:getUserResponse></S:Body></S:Envelope>

这种方式略显麻烦,我们再用httpClient来试试:

public class MyTest3 {    public static void main(String[] args) {
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost("http://127.0.0.1:9999/helloService?wsdl");
post.addHeader("Content-Type", "text/xml;charset=utf-8");
StringBuffer sb = new StringBuffer();
sb.append("<S:Envelope xmlns:S=")
.append("\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body>")
.append("<ns2:getUser xmlns:ns2=\"http://webservice.lenve/\">")
.append("</ns2:getUser></S:Body></S:Envelope>");
post.setEntity(new StringEntity(sb.toString()));
CloseableHttpResponse resp = httpClient.execute(post);
if (resp.getStatusLine().getStatusCode()>199 && resp.getStatusLine().getStatusCode() < 400) {
HttpEntity entity = resp.getEntity();
String result = EntityUtils.toString(entity);
System.out.println(result);
}
resp.close();
} catch (ParseException | IOException e) {
e.printStackTrace();
}
}
}

httpClient是apache提供的用来联网的工具类,操作很方便,输出结果是一样的:

<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:getUserResponse xmlns:ns2="http://webservice.lenve/"><return><nickname>zhangsan</nickname><username>张三</username></return></ns2:getUserResponse></S:Body></S:Envelope>

这种调用方式是我们自己构建一段xml代码来调用webservice服务。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292