因为本人日常的调用地址,很多时候都是wsdl这种的,还需要XML格式的传参,测试的时候用到soapUI测试工具,传参方式是:
<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:impl=\"http://impl.applogic.webServices.card.simis.si.neusoft.com\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <impl:transBusinessForCard soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
" <keyInfo xsi:type=\"xsd:string\">"+keyInfo+"</keyInfo>\n" +
" <conInfo xsi:type=\"xsd:string\">"+conInfo+"</conInfo>\n" +
" <operType xsi:type=\"xsd:string\">"+operType+"</operType>\n" +
" <busType xsi:type=\"xsd:string\">"+busType+"</busType>\n" +
" </impl:transBusinessForCard>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>
我是第一次用到soapUI这个工具,发现测试的时候参数需要放到这个规定的格式里面,并且需要将这一坨全部当参数传输过去才是真正的参数,被调用的接口才能解析,所以我就直接写了一个工具类,也是参考其他博主帖子弄个出来的,上代码:
public class WebServiceUtil {
public static String connWsdl(String httpUrl,String keyInfo,String conInfo,String operType,String busType) throws IOException {
//第一步:创建服务地址,也就是提供的WSDL的接口地址
URL url = new URL(httpUrl);
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
connection.setRequestProperty("SOAPAction", "actionName");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:组织SOAP数据,发送请求,
String soapXML = getXML(keyInfo,conInfo,operType,busType);
//将信息以流的方式发送出去
connection.connect();
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes("UTF-8"));
os.flush();
os.close();
String str = null;
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
if(200 == responseCode){//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is,"UTF-8");
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
str = sb.toString();
System.out.println(sb.toString());
is.close();
isr.close();
br.close();
}
os.close();
return str;
}
//请求入参
public static String getXML(String keyInfo,String conInfo,String operType,String busType){
String soapXML = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:impl=\"http://impl.applogic.webServices.card.simis.si.neusoft.com\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <impl:transBusinessForCard soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
" <keyInfo xsi:type=\"xsd:string\">"+keyInfo+"</keyInfo>\n" +
" <conInfo xsi:type=\"xsd:string\">"+conInfo+"</conInfo>\n" +
" <operType xsi:type=\"xsd:string\">"+operType+"</operType>\n" +
" <busType xsi:type=\"xsd:string\">"+busType+"</busType>\n" +
" </impl:transBusinessForCard>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
return soapXML;
}
}
如果有侵权到任何一个博主的博客,请联系删除~~,谢谢!