目录
Java发起SOAP请求代码参考
- 代码
- 1.组装参数
- 2.加密参数
- 3.发起连接
- 4.解析返回数据
- 参考
文章所属专区 超链接
代码
1.组装参数
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config/>
<context:component-scan base-package="lu.tool">
<!--将Controller的注解排除掉 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<aop:aspectj-autoproxy/>
</beans>
2.加密参数
//对密钥进行MD5
String md5 = Md5Util.getMD5(private_key);
//sm4对数据加密
String xmlStrSM4=new SM4().encode(xmlStr.toString(),md5 );
//对接方传递公钥给我方,我方会根据对接方的公钥查询出密钥对数据解密
xmlStrSM4=xmlStrSM4.replaceAll("[\\n\\r]", "");
JSONObject jsonObject = getUrlBySoap( token, appKey, xmlStrSM4);
3.发起连接
/**
* 根据soap请求获取url
* @param token
* @param xmlStr
* @return
* @throws ServiceException
*/
public String getUrlBySoap(String token,String xmlStr) throws ServiceException {
String endpoint=evaluation_url;
Service service = new Service();
Call call = (Call) service.createCall();
SOAPHeaderElement head = new SOAPHeaderElement("http://linewell.com/ws/", "Authorization", "Basic " + token);
call.addHeader(head);
call.setTargetEndpointAddress(endpoint);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
String result = getSaveEvaluationUrl(call,xmlStr);
return result;
}
public String getSaveEvaluationUrl(Call c ,String xmlStr) {
c.addParameter("appKey", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);// 接口的参数
c.addParameter("xmlStr", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);// 接口的参数
c.setOperationName("getSaveEvaluationUrl");// WSDL里面描述的接口名称 newSubmit submit
String appKey= public_key;//公钥
String result = "";
try{
result=(String)c.invoke(new Object[] {appKey,xmlStr});
}catch(Exception e){
String errStr=getStackTraceInfo(e);
//请记录日志
//log.error("你们能标识这条记录的表示:"+id+"错误信息:"+errStr);
System.out.println("你们能标识这条记录的表示:错误信息:"+errStr);
if(errStr.contains("access_token_invalid")){
System.out.println("请重新获取token并重新请求接口");
//token失效,请重新获取token并且重新请求接口,
//同一个标识请求都报这个三次以内重新请求获取,超过3次不用再请求,找运维人员反馈就行
}
}
return result;
}
4.解析返回数据
public JSONObject getResultUrl(String soapUrl) throws DocumentException {
JSONObject result = new JSONObject();
//报文转成doc对象
Document doc = DocumentHelper.parseText(soapUrl);
//获取根元素,准备递归解析这个XML树
Element root = doc.getRootElement();
//获取叶子节点的方法
String leafNode = "";
leafNode = getCode(root);
if(leafNode != null){
String resultUrl = leafNode;
result.put("url",resultUrl);
}
return result;
}
/**
* 找到soap的xml报文的叶子节点的数据
* @param root
*/
public String getCode(Element root) throws DocumentException {
String result = "";
if (root.elements() != null) {
//如果当前跟节点有子节点,找到子节点
List<Element> list = root.elements();
//遍历每个节点
for (Element e : list) {
if (e.elements().size() > 0) {
//当前节点不为空的话,递归遍历子节点;
result=getCode(e);
if(result != null && result != ""){
return result;
}
}
if (e.elements().size() == 0) {
String a2 = e.getQName().getName();
if(a2.equals("url")){
result = e.getTextTrim();
return result;
}
}
}
}else{
return root.getTextTrim();
}
return result;
}
参考
Java生成Soap请求响应实体,Java 生成wsdl请求响应实体
SOAP请求方式
SOAP请求详解
给个三连吧 谢谢谢谢谢谢了