之前调用webservice接口,生成客户端代码比较麻烦。通过httpclient方式直接调用,soapRequestData 实际上是拼接的xml。
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020071509493828.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MDkwNjM1Mw==,size_16,color_FFFFFF,t_70#pic_center)
方式如下:
String responseMsg = ""; // 返回
SimpleDateFormat myFmt1=new SimpleDateFormat("yyyy-MM-dd"); //格式化年月日
StringBuffer soapRequestData = new StringBuffer(""); // 拼接SOAP
soapRequestData.append("<soapenv:Envelope xmlns:soapenv=\"http://??/envelope/\" xmlns:api=\"http://??.com/\">");
soapRequestData.append("<soapenv:Header/>");
soapRequestData.append("<soapenv:Body>");
soapRequestData.append("<api:??>");
soapRequestData.append("<arg0>");
soapRequestData.append("<传参??AuthTime>"+myFmt1.format(new Date())+"</传参??AuthTime>"); //审批时间
soapRequestData.append("<传参??Opinion>"+activitiDto.getReviewDesc()+"</legOpinion>"); //审批意见描述
soapRequestData.append("<传参??Status>PASSED</传参??Status>"); //同意--通过
soapRequestData.append("<传参??UserCode>"+activitiDto.getReviewSn()+"</传参??UserCode>"); //审核人编码
soapRequestData.append("<传参??UserName>"+activitiDto.getReviewUser()+"</传参??UserName>");//审核人名称
soapRequestData.append("<传参??Code>"+contractSeal.getApplyNum()+"</传参??Code>"); //申请单号
String viewUrlNew= viewUrl.replace("&","&"); //将盖完章的文件地址中&进行转义&
soapRequestData.append("<传参??FileUrl>"+viewUrlNew+"</传参??FileUrl>"); //盖完章的url地址
soapRequestData.append("</arg0>");
soapRequestData.append("</api:??>");
soapRequestData.append("</soapenv:Body>");
soapRequestData.append("</soapenv:Envelope>");
String url =null;
//获取字典表中维护的接口信息
JsonResult<Dictionary> result = dicClient.getRecordById("url");
if (result.getCode() == 0) {
url = result.getData().getDicValue();//获取接口地址
}
//client方式调用接口
PostMethod postMethod = new PostMethod(url);
RequestEntity re = new StringRequestEntity(soapRequestData.toString(), "text/xml", "UTF-8");
postMethod.setRequestEntity(re);
HttpClient httpClient = new HttpClient();
httpClient.executeMethod(postMethod);
responseMsg = postMethod.getResponseBodyAsString().trim();
//如果调用接口没有返回操作成功,表示调用接口失败,直接抛出异常到页面,给用户进行提示
if(!responseMsg.contains("操作成功")) {
throw new Exception("调用接口失败");
}
学习参照:https://www.cnblogs.com/feifeicui/p/9029806.html