这是我在做对外部系统推送数据时自己写的WebService推送接口工具类,有几点需要注意
1、我们调用对方的WebService接口,对方会给一个WebService接口的地址,供我们访问:http://localhost:8080/cgs-ui/services/NC_TFM_INF_FundService?wsdl
直接访问这个地址之后我们看到对方接口中可以供调用的方法名,如下:
2、具体的调用都在下方代码中,代码中的一些参数,都有注释
1 package com.ritoinfo.tf2m.arapPayment.util;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import org.apache.commons.httpclient.HttpClient;
10 import org.apache.commons.httpclient.HttpException;
11 import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
12 import org.apache.commons.httpclient.methods.PostMethod;
13 import org.apache.commons.httpclient.methods.RequestEntity;
14 import org.apache.commons.httpclient.params.HttpMethodParams;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 import com.longtop.tfm.bas.util.UtilString;
19
20 /**
21 * @Description
22 * @author
23 * @version
24 */
25 public class WebServiceUtil {
26
27 public static final Log log = LogFactory.getLog(WebServiceUtil.class);
28
29 /**
30 * @Title: send
31 * @Description: 发送请求
32 * @param @param map
33 * @param @return 参数
34 * @return Map 返回类型
35 * @throws
36 */
37 public static Map send(Map map){
38
39 //组装报文
40 String sendXml = getSoapMsg(map);
41 log.error("发送出去的报文:" + sendXml);
42
43 // 创建httpClient实例对象
44 HttpClient httpClient = new HttpClient();
45 // 设置httpClient连接主机服务器超时时间:15000毫秒
46 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
47 // 创建post请求方法实例对象
48 PostMethod postMethod = new PostMethod((String) map.get("path"));
49 // 设置post请求超时时间
50 postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
51 // 在头文件中设置转码
52 postMethod.addRequestHeader("Content-Type","text/xml;charset=UTF-8");
53 Map returnMap = new HashMap
package com.ritoinfo.tf2m.arapPayment.util;
import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.Map;
import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.methods.InputStreamRequestEntity;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.methods.RequestEntity;import org.apache.commons.httpclient.params.HttpMethodParams;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;
import com.longtop.tfm.bas.util.UtilString;
/*** @Description
* @author * @version */public class WebServiceUtil {public static final Log log = LogFactory.getLog(WebServiceUtil.class);/** * @Title: send * @Description: 发送请求 * @param @param map * @param @return 参数 * @return Map 返回类型 * @throws */public static Map send(Map map){//组装报文String sendXml = getSoapMsg(map);log.error(“发送出去的报文:” + sendXml);// 创建httpClient实例对象 HttpClient httpClient = new HttpClient(); // 设置httpClient连接主机服务器超时时间:15000毫秒 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000); // 创建post请求方法实例对象 PostMethod postMethod = new PostMethod((String) map.get(“path”)); // 设置post请求超时时间 postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);// 在头文件中设置转码 postMethod.addRequestHeader(“Content-Type”,”text/xml;charset=UTF-8″); Map returnMap = new HashMap(); try { //然后把Soap请求数据添加到PostMethod中 byte[] b = null; b = sendXml.getBytes(“utf-8”); InputStream is = new ByteArrayInputStream(b, 0, b.length); RequestEntity re = new InputStreamRequestEntity(is, b.length); postMethod.setRequestEntity(re); // 发送请求httpClient.executeMethod(postMethod);// 返回的结果String result = postMethod.getResponseBodyAsString();map.put(“result”, result);// 将返回报文中的转义字符进行转换returnMap = analysisXml(map);} catch (HttpException e) {log.error(“发送HTPP请求报错!!!”);e.printStackTrace();} catch (IOException e) {log.error(“发送HTPP请求报错!!!”);e.printStackTrace();} postMethod.releaseConnection();
return returnMap;}/** * @Title: getSoapMsg * @Description: 组装发送的soapUI报文 * 具体的WebService的发送的报文需要接收放提供模板,然后将下面的报文进行改进 * methodName是我们调用对方WebService接口需要调用的方法名,在对方的wsdl文件中也会有体现的 * @param @param map * @param @return 参数 * @return String 返回类型 * @throws */private static String getSoapMsg(Map map) {StringBuffer sb = new StringBuffer();sb.append(“”);sb.append(“”);sb.append(“”);sb.append(“”);sb.append(“”);sb.append(“”);sb.append(“ ”);sb.append(“ ”);sb.append(“ ”);sb.append(“ ”);return sb.toString();}/** * @Title: analysisXml * @Description: 转义特殊字符并将结果输出 * @param @param map * @param @return 参数 * @return Map 返回类型 * @throws */private static Map analysisXml(Map map){Map returnMap = new HashMap();String result = (String) map.get(“result”);if(UtilString.isNotEmpty(result)){result = result.replaceAll(“<”,”<“); result = result.replaceAll(“>”,”>”); result = result.replaceAll(“'”,”‘”); result = result.replaceAll(“"”,”\””); result = result.replaceAll(“
”,”\r”); result = result.replaceAll(“&”,” “); result = result.replaceAll(“ ”,” “);int statusBegin = result.indexOf(“”); int statusEnd = result.indexOf(“ ”); String status = result.substring((statusBegin + “”.length()),statusEnd); int messageBegin = result.indexOf(“”); int messageEnd = result.indexOf(“ ”); String message = result.substring((messageBegin + “”.length()),messageEnd); returnMap.put(“status”, status); returnMap.put(“message”, message); returnMap.put(“result”, result);}return returnMap;}}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/135168.html