
原文转载https://www.cnblogs.com/coshaho/p/5689738.htmlpublic class WsdlInfo{private String wsdlName;private ListInterfaceInfointerfaces;/** * coshaho * param path wsdl地址 * throws Exception */ public WsdlInfo(String path)throws Exception{WsdlProject projectnew WsdlProject();WsdlInterface[]wsdlInterfacesWsdlImporter.importWsdl(project, path);this.wsdlNamepath;if(null!wsdlInterfaces){ListInterfaceInfointerfacesnew ArrayListInterfaceInfo();for(WsdlInterface wsdlInterface:wsdlInterfaces){InterfaceInfo interfaceInfonew InterfaceInfo(wsdlInterface);interfaces.add(interfaceInfo);}this.interfacesinterfaces;}}public StringgetWsdlName(){returnwsdlName;}public void setWsdlName(String wsdlName){this.wsdlNamewsdlName;}public ListInterfaceInfogetInterfaces(){returninterfaces;}public void setInterfaces(ListInterfaceInfointerfaces){this.interfacesinterfaces;}}public class InterfaceInfo{private String interfaceName;private ListOperationInfooperations;private String[]adrress;public InterfaceInfo(WsdlInterface wsdlInterface){this.interfaceNamewsdlInterface.getName();this.adrresswsdlInterface.getEndpoints();int operationNumwsdlInterface.getOperationCount();ListOperationInfooperationsnew ArrayListOperationInfo();for(int i0;ioperationNum;i){WsdlOperation operation(WsdlOperation)wsdlInterface.getOperationAt(i);OperationInfo operationInfonew OperationInfo(operation);operations.add(operationInfo);}this.operationsoperations;}public StringgetInterfaceName(){returninterfaceName;}public void setInterfaceName(String interfaceName){this.interfaceNameinterfaceName;}public ListOperationInfogetOperations(){returnoperations;}public void setOperations(ListOperationInfooperations){this.operationsoperations;}public String[]getAdrress(){returnadrress;}public void setAdrress(String[]adrress){this.adrressadrress;}}public class OperationInfo{private String operationName;private String requestXml;private String responseXml;public OperationInfo(WsdlOperation operation){operationNameoperation.getName();requestXmloperation.createRequest(true);responseXmloperation.createResponse(true);}public StringgetOperationName(){returnoperationName;}public void setOperationName(String operationName){this.operationNameoperationName;}public StringgetRequestXml(){returnrequestXml;}public void setRequestXml(String requestXml){this.requestXmlrequestXml;}public StringgetResponseXml(){returnresponseXml;}public void setResponseXml(String responseXml){this.responseXmlresponseXml;}}public class WSDLParseTest{public static void main(String[]args)throws Exception{String urlhttp://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx?wsdl;WsdlInfo wsdlInfonew WsdlInfo(url);System.out.println(WSDL URL is wsdlInfo.getWsdlName());for(InterfaceInfo interfaceInfo:wsdlInfo.getInterfaces()){System.out.println(Interface name is interfaceInfo.getInterfaceName());for(String ads:interfaceInfo.getAdrress()){System.out.println(Interface address is ads);}for(OperationInfo operation:interfaceInfo.getOperations()){System.out.println(operation name is operation.getOperationName());System.out.println(operation request is );System.out.println(operation request is operation.getRequestXml());System.out.println(operation response is );System.out.println(operation.getResponseXml());}}}}第一中httpURLConnection方式调用package com.ssh.webserviceTSY; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; public class Test { public static void main(String[] args) throws IOException { //第一步创建服务地址 URL url new URL(http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl); //第二步打开一个通向服务地址的连接 HttpURLConnection connection (HttpURLConnection) url.openConnection(); //第三步设置参数 //3.1发送方式设置POST必须大写 connection.setRequestMethod(POST); //3.2设置数据格式content-type connection.setRequestProperty(content-type, text/xml;charsetutf-8); //3.3设置输入输出因为默认新创建的connection没有读写权限 connection.setDoInput(true); connection.setDoOutput(true); //第四步组织SOAP数据发送请求 String soapXML getXML(17321242779); //将信息以流的方式发送出去 OutputStream os connection.getOutputStream(); os.write(soapXML.getBytes()); //第五步接收服务端响应打印 int responseCode connection.getResponseCode(); if(200 responseCode){//表示服务端响应成功 //获取当前连接请求返回的数据流 InputStream is connection.getInputStream(); InputStreamReader isr new InputStreamReader(is); BufferedReader br new BufferedReader(isr); StringBuilder sb new StringBuilder(); String temp null; while(null ! (temp br.readLine())){ sb.append(temp); } /** * 打印结果 */ System.out.println(sb.toString()); is.close(); isr.close(); br.close(); } os.close(); } public static String getXML(String phone){ String soapXML ?xml version\1.0\ encoding\utf-8\? soap:Envelope xmlns:xsi\http://www.w3.org/2003/XMLSchema-instance\ xmlns:web\http://WebXml.com.cn/\ xmlns:xsd\http://www.w3.org/2003/XMLSchema\ xmlns:soap\http://schemas.xmlsoap.org/soap/envelope/\ soap:Body web:getMobileCodeInfo phone /web:getMobileCodeInfo /soap:Body /soap:Envelope; return soapXML; } }第二中httpURLConnection方式调用package com.zyh.car.util; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import javax.annotation.Resource; public class GetEquipMsgUtils { public static void main(String[] args) throws IOException { //第一步创建服务地址也就是提供的WSDL的接口地址 URL url new URL(http://xxxxxxxxxxxxxxxx/xxxx/xxxxxxxxx?wsdl); //第二步打开一个通向服务地址的连接 HttpURLConnection connection (HttpURLConnection) url.openConnection(); //第三步设置参数 //3.1发送方式设置POST必须大写 connection.setRequestMethod(POST); //3.2设置数据格式content-type connection.setRequestProperty(Content-Type, text/xml;charsetUTF-8); //3.3设置输入输出因为默认新创建的connection没有读写权限 connection.setDoInput(true); connection.setDoOutput(true); //第四步组织SOAP数据发送请求 String soapXML getXML(方法名,参数1,参数2); //将信息以流的方式发送出去 OutputStream os connection.getOutputStream(); os.write(soapXML.getBytes()); //第五步接收服务端响应打印 int responseCode connection.getResponseCode(); if(200 responseCode){//表示服务端响应成功 //获取当前连接请求返回的数据流 InputStream is connection.getInputStream(); InputStreamReader isr new InputStreamReader(is); BufferedReader br new BufferedReader(isr); StringBuilder sb new StringBuilder(); String temp null; while(null ! (temp br.readLine())){ sb.append(temp); } 或者 int BUFFER_SIZE 1024; char[] buffer new char[BUFFER_SIZE]; // or some other size, int charsRead 0; while ( (charsRead br.read(buffer, 0, BUFFER_SIZE)) ! -1) { sb.append(buffer, 0, charsRead); } /** * 打印结果 */ System.out.println(---sb.toString()); try{ //对返回的数据进行JSON格式化 System.out.println(xml2Json(sb.toString())); }catch (Exception e){ e.printStackTrace(); } is.close(); isr.close(); br.close(); } os.close(); } //请求入参 public static String getXML(String method,int begin,int end){ String soapXML soapenv:Envelope xmlns:soapenv\http://schemas.xmlsoap.org/soap/envelope/\ xmlns:tns\http://guizhou.fire.and.rescue\ soapenv:Header/soapenv:Header soapenv:Body tns:method //方法名 tns:offsetbegin/tns:offset//参数1 tns:row_countend/tns:row_count//参数2 /tns:method /soapenv:Body /soapenv:Envelope; return soapXML; } /** * xml转json * * param xmlStr * return * throws DocumentException */ public static JSONObject xml2Json(String xmlStr) throws DocumentException { Document doc DocumentHelper.parseText(xmlStr); JSONObject json new JSONObject(); dom4j2Json(doc.getRootElement(), json); return json; } /** * xml转json * * param element * param json */ public static void dom4j2Json(Element element, JSONObject json) { ListElement chdEl element.elements(); for(Element e : chdEl){ if (!e.elements().isEmpty()) { JSONObject chdjson new JSONObject(); dom4j2Json(e, chdjson); Object o json.get(e.getName()); if (o ! null) { JSONArray jsona null; if (o instanceof JSONObject) { JSONObject jsono (JSONObject) o; json.remove(e.getName()); jsona new JSONArray(); jsona.add(jsono); jsona.add(chdjson); } if (o instanceof JSONArray) { jsona (JSONArray) o; jsona.add(chdjson); } json.put(e.getName(), jsona); } else { if (!chdjson.isEmpty()) { json.put(e.getName(), chdjson); } } } else { if (!e.getText().isEmpty()) { json.put(e.getName(), e.getText()); } } } } }