1. 程式人生 > 其它 >JAVA學習之SoapUI報文傳送和解析

JAVA學習之SoapUI報文傳送和解析

JAVA、SoapUI、報文、Document、dom4j

SoapUI工具測試

1、新建專案

2、編寫名稱和填寫url路徑,點選Ok

測試url:http://www.webxml.com.cn/webservices/ChinaTVprogramWebService.asmx?wsdl

3、軟體會自動建立demo,所以可以直接開啟報文demo組裝資訊

補充:工具使用詳情請參考https://my.oschina.net/u/4306990/blog/3913915?hmsr=kaifa_aladdin

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;
    }