package www.codepeople.cn.test.utils; import java.io.ByteArrayInputStream; import java.io.InputStream; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.InputStreamRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; /** * @ClassName: DynamicHttpclientCall * @Description: 靜態調用wsdl * @author 劉仁 * @date 2017年6月15日 下午6:44:35 */ // 動態構造調用串,靈活性更大 public class DynamicHttpclientCall { private String namespace; private String methodName; private String wsdlLocation; private String soapResponseData; public DynamicHttpclientCall(String namespace, String methodName, String wsdlLocation) { this.namespace = namespace; this.methodName = methodName; this.wsdlLocation = wsdlLocation; } private int invoke() throws Exception { PostMethod postMethod = new PostMethod(wsdlLocation); String soapRequestData = buildRequestData(); byte[] bytes = soapRequestData.getBytes("UTF-8"); InputStream inputStream = new ByteArrayInputStream(bytes, 0, bytes.length); RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, bytes.length, "text/xml; charset=UTF-8"); postMethod.setRequestEntity(requestEntity); HttpClient httpClient = new HttpClient(); int statusCode = httpClient.executeMethod(postMethod); soapResponseData = postMethod.getResponseBodyAsString(); return statusCode; } private String buildRequestData() { StringBuffer soapRequestData = new StringBuffer(); soapRequestData .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<soapenv:Body><ns1:SMSTest soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" " + "xmlns:ns1=\"http://service.hoo.com/\"><xml xsi:type=\"xsd:string\">" + "<?xml version="1.0" encoding="UTF-8"?>" + "<SMSReq>" + "<SMS_ID>123456</SMS_ID>" + "<MDN>18689653751</MDN>" + "<ASSIGN_TIME>20170615175307</ASSIGN_TIME>" + "<WORKSHEET_PERSON>张三</WORKSHEET_PERSON>" + "<SMS_INFO>欠费了</SMS_INFO>" + "</SMSReq>" + "</xml></ns1:SMSTest></soapenv:Body></soapenv:Envelope>"); return soapRequestData.toString(); } /** * @Title: main * @Description: http調用wsdl協議 * @param @param args * @param @throws Exception 設定文件 * @return void 返回類型 * @throws */ public static void main(String[] args) throws Exception { DynamicHttpclientCall dynamicHttpclientCall = new DynamicHttpclientCall("http://service.hoo.com/", "SMSTest", "http://127.0.0.1:8080/STEInterface/STEservice?wsdl"); String soapRequestData = dynamicHttpclientCall.buildRequestData(); System.out.println(soapRequestData); int statusCode = dynamicHttpclientCall.invoke(); if (statusCode == 200) { System.out.println("調用成功!"); System.out.println(dynamicHttpclientCall.soapResponseData); } else { System.out.println("調用失敗!錯誤碼:" + statusCode); } } }
須要用到的架包java
commons-codec-1.4.jar
commons-httpclient-3.0.1.jar
commons-logging-1.1.1.jarweb
使用Axis1調用wsdl協議apache
package com.tydic.web.cust.common; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.encoding.XMLType; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.log4j.Logger; import com.tydic.base.util.IMapEntry; import com.tydic.base.util.Maps; /** * 短信發送接口 * @author liure * */ public class SMSSendUtil { private static final Logger LOGGER = Logger.getLogger(MsgSendUtil.class); private static Properties properties = new Properties(); private static String propertyName = "mail_msg.properties"; public final static String FILE_SEP = System.getProperty("file.separator");//文件分隔符(在 UNIX 系統中是「/」) private static String wsdlUrl; private static String wsdlQName; private static String wsdlMethod; static { loadConfigProperties(); } /** * @Title: getPropertyPath * @Description: 獲取配置文件 * @param @return 設定文件 * @return String 返回類型 * @throws */ public static String getPropertyPath(){ String path = System.getProperty("user.dir")+FILE_SEP+"config"+FILE_SEP+propertyName; LOGGER.info(path); return propertyName; } /** * Load configuration properties to initialize attributes. * @throws Exception */ private static void loadConfigProperties() { try { Properties prop = new Properties(); InputStream inStream = MsgSendUtil.class.getClassLoader().getResourceAsStream(getPropertyPath());//包內配置文件 prop.load(inStream); wsdlUrl = prop.getProperty("WSDLURL"); wsdlQName = prop.getProperty("QNAME"); wsdlMethod = prop.getProperty("METHOD"); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String format_time = getTime(); System.out.println(format_time); String SMS_ID = "123456"; String worksheet_person = "張三"; // 短信發送方信息 String sms_info = "欠費了";//短信內容 List<String> phoneNumList = new ArrayList<String>(); phoneNumList.add("18789xxxxx"); phoneNumList.add("186896xxxx"); for (String mdn : phoneNumList) { String xmlString = NamespaceURI_R(SMS_ID, mdn, format_time, worksheet_person, sms_info); sendSMS(xmlString); } } /** * 發送短信的wsdl接口 * * @param xmlString */ public static void sendSMS(String xmlString) { IMapEntry<String,Object> resultMap = Maps.newMapEntry(); try { String msg = ""; Map<String,Object> map_read = new HashMap<String, Object>(); Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(wsdlUrl); // WSDL裏面描述的接口名稱(要調用的方法) call.setOperationName(new QName(wsdlQName, wsdlMethod)); call.addParameter("xml", XMLType.XSD_STRING, ParameterMode.IN); // 封裝參數 // 設置被調用方法的返回值類型 call.setReturnType(XMLType.XSD_STRING); // 設置方法中參數的值 Object[] paramValues = new Object[] { xmlString }; LOGGER.info("短信接口傳入報文:"); LOGGER.info(xmlString); // 給方法傳遞參數,而且調用方法 String result = (String) call.invoke(paramValues); LOGGER.info("短信接口返回報文:"); LOGGER.info(result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 初始化時間格式 * * @return */ private static String getTime() { Date date = new Date(); SimpleDateFormat sft = new SimpleDateFormat("yyyyMMddHHmmss"); String time = sft.format(date); return time; } /** * 封裝短信發送的xml報文 * * @param SMS_ID * 發送短信的ID;數據類型:String;長度:100 * @param MDN * 接收短信的手機號碼;數據類型:String;長度:11 * @param time * 短信派發時間,格式爲:yyyyMMddHHmmss;數據類型:String;長度:14 * @param phoneNum * @param msg * @return */ public static String NamespaceURI_R(String SMS_ID, String mdn, String time, String WORKSHEET_PERSON, String SMS_INFO) { StringBuffer buffer = new StringBuffer(); buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); buffer.append("<SMSReq>"); buffer.append("<SMS_ID>"); buffer.append(SMS_ID); buffer.append("</SMS_ID>"); buffer.append("<MDN>"); buffer.append(mdn); buffer.append("</MDN>"); buffer.append("<ASSIGN_TIME>"); buffer.append(time); buffer.append("</ASSIGN_TIME>"); buffer.append("<WORKSHEET_PERSON>"); buffer.append(WORKSHEET_PERSON); buffer.append("</WORKSHEET_PERSON>"); buffer.append("<SMS_INFO>"); buffer.append(SMS_INFO); buffer.append("</SMS_INFO>"); buffer.append("</SMSReq>"); return buffer.toString(); } }
須要的架包:app
<!-- wsdl調用方法 --> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>axis</groupId> <artifactId>axis-jaxrpc</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.2</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <!-- wsdl調用end -->