使用HTTPClient的方式编写Webservice客户端

xu.wang

发布于 2020.03.25 00:57 阅读 2560 评论 0

    在上篇文章中编写了webservice的客户端,但是在浏览器中输入webservice地址只是可以测试webserve是否启动,想要测试webservice服务端是否有问题,则需要使用客户端进行测试。

    本篇文章记录使用HTTPclient的方式请求webservice服务,本方法优点是不用生成webservice文件,直接发送和接口xml即可;缺点是需要手动拼写xml,相对比较麻烦。

首先,确定xml格式以及内容

 public static String getXML(){
        String soapXML = "<?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>"
                +"<method:SuperAndonBarcodeReceive xmlns:method=\"http://service.jtexplorer.com/\">"
                +"<barcode>dasdad</barcode>"
                +"</method:SuperAndonBarcodeReceive>"
                +"</soap:Body>"
                +"</soap:Envelope>";

        System.out.println(soapXML);
        return soapXML;
    }

其中需要注意的是格式问题:

1. “method:”后面需要填写接口中方法的名字;

2. 在method元素中 xmlns:method=""中的链接必须为在浏览器中请求webservice链接后得到的xml中namespace的元素值;

3. 在<method:SuperAndonBarcodeReceive></method:SuperAndonBarcodeReceive>元素中间的元素为接口中参数的名字例如:<barcode>参数值</barcode>,参数可添加多个;

除此之外其他地方最好不要修改。

第二步:编写http请求

 @Test
    public void http() throws IOException {
        //第一步:创建服务地址
        URL url = new URL("http://localhost:8080/webservice/HelloWorld?wsdl");
        //第二步:打开一个通向服务地址的连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //第三步:设置参数
        //3.1发送方式设置:POST必须大写
        connection.setRequestMethod("POST");
        //3.2设置数据格式:content-type
        connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
        //3.3设置输入输出,因为默认新创建的connection没有读写权限,
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //第四步:组织SOAP数据,发送请求
        String soapXML = getXML();
        OutputStream os = connection.getOutputStream();
        os.write(soapXML.getBytes());

        //第五步:接收服务端响应,打印(xml格式数据)
        int responseCode = connection.getResponseCode();

        System.out.println(connection.getResponseMessage());

        if(200 == responseCode){//表示服务端响应成功
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            StringBuilder sb = null;
            try {
                is = connection.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);

                sb = new StringBuilder();
                String temp = null;
                while(null != (temp = br.readLine())){
                    sb.append(temp);
                }
                System.out.println(sb.toString());
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {
                br.close();
                isr.close();
                is.close();
            }
        }

        os.close();
    }

返回值如下:

发送的参数:<?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><method:SuperAndonBarcodeReceive xmlns:method="http://service.jtexplorer.com/"><barcode>dasdad</barcode></method:SuperAndonBarcodeReceive></soap:Body></soap:Envelope>
状态:OK
返回值:<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:SuperAndonBarcodeReceiveResponse xmlns:ns2="http://service.jtexplorer.com/"><return>this send barchode is : dasdad</return></ns2:SuperAndonBarcodeReceiveResponse></soap:Body></soap:Envelope>

Process finished with exit code 0