Java学习之SoapUI报文发送和解析

SoapUI工具测试

1、新建项目

 

2、编写名称和填写url路径,点击Ok

测试url:http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?wsdl 

这是个测试获取电视节目的url

3、软件会自动创建demo,所以可以直接打开报文demo组装信息

Java代码实现

1、组装报文

    public static String getMessage() {
        StringBuffer buff = new StringBuffer();
        buff.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://WebXml.com.cn/\">");
        buff.append("<soapenv:Header/>");
        buff.append("<soapenv:Body>");
        buff.append("<web:getAreaString/>");
        buff.append("</soapenv:Body>");
        buff.append("</soapenv:Envelope>");
        return buff.toString();
    }

2、发送并接收请求

 补充:需要pom.xml中引入“dom4j”的依赖

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
public class RequestUtil {
    private final static String REQUEST_METHOD = "POST";

    public static Document sendRequest(String url, String message) {
        URL connect;
        HttpURLConnection connection = null;
        Document resDoc = null;
        if (StringUtils.isEmpty(url)) {
            return null;
        }
        try {
            connect = new URL(url);
            connection = (HttpURLConnection) connect.openConnection();
            connection.setRequestMethod(REQUEST_METHOD);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);// post请求不能使用缓存
            //设置连接超时时间
            connection.setConnectTimeout(2000);
            connection.setReadTimeout(10000);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            OutputStreamWriter outParam = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
            outParam.write(message);
            outParam.flush();
            outParam.close();
            InputStream responseIn = connection.getInputStream();
            SAXReader reader = new SAXReader();
            resDoc = reader.read(responseIn);
            return resDoc;
        } catch (Exception e) {
            log.info(e.getMessage());
        } finally {
            connection.disconnect();
        }
        return resDoc;
    }
}

3、解析返回报文

public class SoapService {

    public void getSoapResponse() {
        String message = RequestUtil.getMessage();
        String url = "http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?wsdl";
        Document document = UrlSendUtil.sendRequest(url, message);
        List<Element> elementList1 = document.getRootElement().elements();
        for (Element element1 : elementList1) {
            List<Element> elementList2 = element1.elements();
            for (Element element2 : elementList2) {
                List<Element> elementList3 = element2.elements();
                for (Element element3 : elementList3) {
                    List<Element> elementList4 = element3.elements();
                    for (Element element4 : elementList4) {
                        System.out.println(element4.getName() + " - " + element4.getStringValue());
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        SoapService soapService = new SoapService();
        soapService.getSoapResponse();
    }
}

结果

string - -4@数字电视@数字
string - -3@海外电视@海外
string - -2@卫星电视@卫星
string - -1@中央电视@中央
string - 1@北京市@华北地区
string - 2@天津市@华北地区
string - 3@河北省@华北地区
string - 4@山西省@华北地区
string - 5@内蒙古自治区@华北地区
string - 6@辽宁省@东北地区
string - 7@吉林省@东北地区
string - 8@黑龙江省@东北地区
string - 9@上海市@华东地区
string - 10@江苏省@华东地区
string - 11@浙江省@华东地区
string - 12@安徽省@华中地区
string - 13@福建省@华南地区
string - 14@江西省@华中地区
string - 15@山东省@华东地区
string - 16@河南省@华中地区
string - 17@湖北省@华中地区
string - 18@湖南省@华中地区
string - 19@广东省@华南地区
string - 20@广西壮族自治区@华南地区
string - 21@海南省@华南地区
string - 22@重庆市@西南地区
string - 23@四川省@西南地区
string - 24@贵州省@西南地区
string - 25@云南省@西南地区
string - 26@西藏自治区@西南地区
string - 27@陕西省@西北地区
string - 28@甘肃省@西北地区
string - 29@青海省@西北地区
string - 30@宁夏回族自治区@西北地区
string - 31@新疆维吾尔自治区@西北地区
string - 32@香港@港澳台地区
string - 33@澳门@港澳台地区
string - 34@台湾省@港澳台地区

补充:因为返回结果不同,而且是从rootElement解析,所以可以写个递归方法,例如:

    public List<Element> getSoapUIResponse(List<Element> elements) {
        for (Element element : elements) {
            if ("string".equals(element.getName())) {//取什么节点的值根据实际业务确定
                return element.elements();
            }
            return getSoapUIResponse(element.elements());
        }
        return null;
    }

 

posted @ 2021-12-07 19:06  请别耽误我写BUG  阅读(2188)  评论(0编辑  收藏  举报