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服务。

posted @ 2015-05-04 16:39  江南一点雨  阅读(884)  评论(0编辑  收藏  举报