一丶说明
1丶此文章基于Java语言来实现WebService接口对接
2丶第三方定义规则, 提供接口文档
二丶SoapUI工具的使用
1丶网上找到破解版的SoapUI工具并安装
2丶根据第三方提供的访问接口地址进行工具的使用, 步骤如下:
(1)新建soap project
(2)输入第三方提供的请求url
(3)获得第三方定义的xml请求格式
注意: soap11和soap12的xml格式不一样
(4)根据文档入参格式,进行测试访问接口
注意: soap11和soap12的请求属性值的定义也一样
三丶工具类定义
import com.thoughtworks.xstream.XStream;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.http.HttpStatus;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public static Object sendOutPut(PlatformAgreementEntity entity, String tranCode, String InBody, Class<?> objClass) {
try {
// 请求信息XML(根据供应商接口文档定义)
String strXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.hip.bi.ecan/\">" +
" <soapenv:Header/>" +
" <soapenv:Body>" +
" <web:getData>" +
" <!--Optional:-->" +
" <data>" +
"<![CDATA[" +
"<EcanHIPRequest>" +
" <Head>" +
" <AppId>"+ entity.getSystemCode() +"</AppId>" +
" <TranCode>"+ tranCode +"</TranCode>" +
" <Token>"+ entity.getSystemToken() +"</Token>" +
" </Head>" +
" <Body>"+ InBody +" </Body>" +
"</EcanHIPRequest>" +
"]]>" +
" </data>" +
" </web:getData>" +
" </soapenv:Body>" +
"</soapenv:Envelope>";
// 请求访问路径
URL url = new URL(entity.getInterfaceUrl());
// 根据URL请求协议生成URLConnection的子类HttpURLConnection以使用子类API
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
// 设置请求方式
httpConn.setRequestMethod("POST");
// setDoOutput()和setDoInput()的作用主要是发送Post请求。 默认为false ,当非GET请求时,需要输入输出参数时,需将其设为true
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
// 设置请求格式\编码语言
byte[] buf = strXml.getBytes(StandardCharsets.UTF_8);
httpConn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("SOAPAction", "");
// 获取输出流对象
OutputStream out = httpConn.getOutputStream();
out.write(buf);
out.close();
// 获取响应状态码
int code = httpConn.getResponseCode();
StringBuilder sb = new StringBuilder();
if (code == HttpStatus.OK.value()) {
InputStream is = httpConn.getInputStream();
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) {
String s = new String(b, 0, len, StandardCharsets.UTF_8);
sb.append(s);
}
is.close();
}
// 关闭连接
httpConn.disconnect();
// xml字符串转义 < 转 <
String xmlStr = sb.toString().replaceAll("<","<");
// XML -> 对象 转换
XStream stream = new XStream();
// XStream设置默认安全防护
//XStream.setupDefaultSecurity(stream);
// 设置允许的类
stream.allowTypesByRegExp(new String[]{".*"});
//xstream使用注解转换 应用传过来的类的注解 xml转换为对象时使用
stream.processAnnotations(objClass);
// StringEscapeUtils类可以对html js xml sql 等代码进行转义来防止SQL注入及XSS注入
String response = StringEscapeUtils.unescapeHtml(xmlStr);
response = response.substring(response.indexOf("<returnContent>"), response.indexOf("</return>"));
// 转换对象
stream.toXML(objClass.newInstance());
// 将XML反序列化获取对象
return stream.fromXML(response);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
四丶总结
1丶使用工具进行测试与定义工具类获取的返回数据可能不一样
2丶此工具类返回Object, 是将xml转换为对象