发布于2021-06-08 13:58 阅读(405) 评论(0) 点赞(17) 收藏(4)
1丶此文章基于Java语言来实现WebService接口对接
2丶第三方定义规则, 提供接口文档
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, Object obj) {
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(obj.getClass());
// StringEscapeUtils类可以对html js xml sql 等代码进行转义来防止SQL注入及XSS注入
String response = StringEscapeUtils.unescapeHtml(xmlStr);
response = response.substring(response.indexOf("<returnContent>"), response.indexOf("</return>"));
// 将XML反序列化获取对象
return stream.fromXML(response);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
1丶使用工具进行测试与定义工具类获取的返回数据可能不一样
2丶此工具类返回Object, 是将xml转换为对象
原文链接:https://blog.csdn.net/spring_is_coming/article/details/117565414
作者:飞翔公园
链接:http://www.javaheidong.com/blog/article/219619/d2e42740c81dec9dba15/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!