该接口比较特殊,不是通过key value 的形式传参的,而是将参数拼接 使用报文的形式传参的。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:getCard>
<soap:callCard>123456abccde</soap:callCard> //方法名 参数是以特定格式拼接的
</soap:getCard>
</soap:Body>
</soap:Envelope>
封装的webserviceUtils工具类:
public static String doPostSoap(String method, String params) {
Map<String, String> map = new HashMap<String, String>();
//拼接xml请求,带有请求头
//String params = "123456abc";//按照指定格式拼接好的参数
String soapRequestData="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <soap:Body>\n" +
" <soap:getCard>\n" +
" <soap:callCard>"+params+"</soap:callCard>\n" +
" </soap:gettCard>\n" +
" </soap:Body>\n" +
"</soap:Envelope>";
soapRequestData= Convert.toDBC(soapRequestData);
//System.out.println(soapRequestData);
Document document=null;
try {
//比如http://192.177.222.222:8888/services/Service_Name/Function_Name.php?wsdl
PostMethod postMethod = new PostMethod(method);
byte[] b = soapRequestData.getBytes("UTF-8");
InputStream is = new ByteArrayInputStream(b, 0, b.length);
RequestEntity re = new InputStreamRequestEntity(is, b.length, "text/xml; charset=UTF-8");
postMethod.setRequestEntity(re);
HttpClient httpClient = new HttpClient();
int statusCode = httpClient.executeMethod(postMethod);
//200说明正常返回数据
if (statusCode != 200) {
//internet error
System.out.println(statusCode);
}
soapRequestData = postMethod.getResponseBodyAsString();
document= DocumentHelper.parseText(soapRequestData);
//System.out.println(soapRequestData);
} catch (Exception e) {
e.printStackTrace();
}
//使用dom4j解析报文 取出code码
Element root=document.getRootElement();
System.out.println("root:"+root);
//获取根元素下的所有子元素
List<Element> list=root.elements();
list=list.get(0).elements();
String getSwipeCard = list.get(0).elementText("getSwipeCard");
JSONObject jsonObject = JSONObject.parseObject(getSwipeCard);
String s = jsonObject.get("status").toString();
System.out.println(s);
return s;
}
public static void main(String[] args) throws Exception {
String s = doPostSoap("192.177.222.222:8888/services/Service_Name/Function_Name.php?wsdl", "1234abc");
System.out.println(s+"测试");
}
正确返回的报文格式
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:swipeCard" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getCardResponse>
<getCard xsi:type="xsd:string">{"code":"-1"}</getCard>
</ns1:getCardResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
因为返回的是报文格式 所以需要解析xml
我使用的dom4j 依赖如下 具体代码在工具类中
<!-- 解析xml方法-->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>