這是我在做對外部系統推送數據時自己寫的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 <br/> 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<Object, Object>(); 54 try { 55 //然后把Soap請求數據添加到PostMethod中 56 byte[] b = null; 57 b = sendXml.getBytes("utf-8"); 58 InputStream is = new ByteArrayInputStream(b, 0, b.length); 59 RequestEntity re = new InputStreamRequestEntity(is, b.length); 60 postMethod.setRequestEntity(re); 61 // 發送請求 62 httpClient.executeMethod(postMethod); 63 // 返回的結果 64 String result = postMethod.getResponseBodyAsString(); 65 map.put("result", result); 66 // 將返回報文中的轉義字符進行轉換 67 returnMap = analysisXml(map); 68 } catch (HttpException e) { 69 log.error("發送HTPP請求報錯!!!"); 70 e.printStackTrace(); 71 } catch (IOException e) { 72 log.error("發送HTPP請求報錯!!!"); 73 e.printStackTrace(); 74 } 75 postMethod.releaseConnection(); 76 77 return returnMap; 78 } 79 80 /** 81 * @Title: getSoapMsg 82 * @Description: 組裝發送的soapUI報文 83 * 具體的WebService的發送的報文需要接收放提供模板,然后將下面的報文進行改進 84 * methodName是我們調用對方WebService接口需要調用的方法名,在對方的wsdl文件中也會有體現的 85 * @param @param map 86 * @param @return 參數 87 * @return String 返回類型 88 * @throws 89 */ 90 private static String getSoapMsg(Map map) { 91 StringBuffer sb = new StringBuffer(); 92 sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"").append("http://service.webservice.core.data.ridol.ritoinfo.com/").append("\">"); 93 sb.append("<soapenv:Header/>"); 94 sb.append("<soapenv:Body>"); 95 sb.append("<ser:").append(map.get("methodName")).append(">"); 96 sb.append("<xml>"); 97 sb.append("<![CDATA["); 98 // =========要發送的報文=============== 99 sb.append(map.get("xml")); 100 // ================================= 101 sb.append("]]>"); 102 sb.append("</xml>"); 103 sb.append("</ser:").append(map.get("methodName")).append(">"); 104 sb.append("</soapenv:Body>"); 105 sb.append("</soapenv:Envelope>"); 106 return sb.toString(); 107 } 108 109 /** 110 * @Title: analysisXml 111 * @Description: 轉義特殊字符並將結果輸出 112 * @param @param map 113 * @param @return 參數 114 * @return Map 返回類型 115 * @throws 116 */ 117 private static Map analysisXml(Map map){ 118 Map returnMap = new HashMap<Object, Object>(); 119 String result = (String) map.get("result"); 120 if(UtilString.isNotEmpty(result)){ 121 result = result.replaceAll("<","<"); 122 result = result.replaceAll(">",">"); 123 result = result.replaceAll("'","'"); 124 result = result.replaceAll(""","\""); 125 result = result.replaceAll("
","\r"); 126 result = result.replaceAll("&"," "); 127 result = result.replaceAll("	"," "); 128 129 int statusBegin = result.indexOf("<status>"); 130 int statusEnd = result.indexOf("</status>"); 131 String status = result.substring((statusBegin + "<status>".length()),statusEnd); 132 133 int messageBegin = result.indexOf("<message>"); 134 int messageEnd = result.indexOf("</message>"); 135 String message = result.substring((messageBegin + "<message>".length()),messageEnd); 136 returnMap.put("status", status); 137 returnMap.put("message", message); 138 returnMap.put("result", result); 139 } 140 return returnMap; 141 } 142 }
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 <br/>* @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<Object, Object>(); 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("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"").append("http://service.webservice.core.data.ridol.ritoinfo.com/").append("\">");sb.append("<soapenv:Header/>");sb.append("<soapenv:Body>");sb.append("<ser:").append(map.get("methodName")).append(">");sb.append("<xml>");sb.append("<![CDATA[");// =========要發送的報文===============sb.append(map.get("xml"));// =================================sb.append("]]>");sb.append("</xml>");sb.append("</ser:").append(map.get("methodName")).append(">");sb.append("</soapenv:Body>");sb.append("</soapenv:Envelope>");return sb.toString();}/** * @Title: analysisXml * @Description: 轉義特殊字符並將結果輸出 * @param @param map * @param @return 參數 * @return Map 返回類型 * @throws */private static Map analysisXml(Map map){Map returnMap = new HashMap<Object, Object>();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("<status>"); int statusEnd = result.indexOf("</status>"); String status = result.substring((statusBegin + "<status>".length()),statusEnd); int messageBegin = result.indexOf("<message>"); int messageEnd = result.indexOf("</message>"); String message = result.substring((messageBegin + "<message>".length()),messageEnd); returnMap.put("status", status); returnMap.put("message", message); returnMap.put("result", result);}return returnMap;}}
