WebService学习笔记系列(二)
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服务。
蓝天为路,阳光满屋。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?