第一步:WebService框架选择【以CXF为例】
1、下载地址:http://cxf.apache.org/download.html,请事先安装好JDK(本人使用的是apache-cxf-2.7.18,JDK1.7)
2、第二步:解压apache-cxf-2.7.11.zip到指定目录,环境变量设置:创建CXF_HOME并加到path下
3、在cmd下加入wsdl2java–h
显示以上结果,成功
第二步:解析wsdl文件
1、使用短信方提供的wsdl地址,右键另存为wsdl文件(以wsdl.wsdl为例)
2、在cmd输入以下命令
wsdl2java -p com.ucp -d D:\\ucp -all D:\\cxf\\wsdl.wsdl 根据D:\\cxf\\wsdl.wsdl文件生成代码,com.ucp是指定包名,方便复制到实际项目中
3、将生成的代码拷贝到实际项目中
第三步:代码导入
一般只需要用到这个ExtCommandServicePortType_ExtCommandServiceHttpPort_Client.java类
package com.ucp; /** * Please modify this class to meet your needs * This class is not complete */ import java.io.File; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; /** * This class was generated by Apache CXF 2.7.18 * 2017-10-12T11:23:03.075+08:00 * Generated source version: 2.7.18 * */ public final class ExtCommandServicePortType_ExtCommandServiceHttpPort_Client { private static final QName SERVICE_NAME = new QName("http://service.message.wiscom.com", "ExtCommandService"); private ExtCommandServicePortType_ExtCommandServiceHttpPort_Client() { } public static void main(String args[]) throws java.lang.Exception { URL wsdlURL = ExtCommandService.WSDL_LOCATION; if (args.length > 0 && args[0] != null && !"".equals(args[0])) { File wsdlFile = new File(args[0]); try { if (wsdlFile.exists()) { wsdlURL = wsdlFile.toURI().toURL(); } else { wsdlURL = new URL(args[0]); } } catch (MalformedURLException e) { e.printStackTrace(); } } ExtCommandService ss = new ExtCommandService(wsdlURL, SERVICE_NAME); ExtCommandServicePortType port = ss.getExtCommandServiceHttpPort(); { System.out.println("Invoking createMo..."); java.lang.String _createMo_in0 = ""; java.lang.String _createMo_in1 = ""; java.lang.String _createMo_in2 = ""; java.lang.Long _createMo__return = port.createMo(_createMo_in0, _createMo_in1, _createMo_in2); System.out.println("createMo.result=" + _createMo__return); } { System.out.println("Invoking createMessage..."); com.ucp.ArrayOfString _createMessage_in0 = null; java.lang.String _createMessage_in1 = ""; java.lang.String _createMessage_in2 = ""; java.lang.String _createMessage_in3 = ""; java.lang.Long _createMessage__return = port.createMessage(_createMessage_in0, _createMessage_in1, _createMessage_in2, _createMessage_in3); System.out.println("createMessage.result=" + _createMessage__return); } System.exit(0); } }
第四步:根据代码的接口调用
推荐使用配置文件加载参数
private static final Logger logger = Logger.getLogger("DefaultSmsSender"); private static String ucpSmsUrl; private static String ucpMsg; private static String apiKey; private static final QName SERVICE_NAME = new QName("http://service.message.wiscom.com", "ExtCommandService"); static { Properties prop = new Properties(); try { prop.load(UcpSmsSender.class.getResourceAsStream("/registerConf.properties")); ucpSmsUrl=prop.getProperty("ucpSmsUrl"); apiKey=prop.getProperty("apiKey"); } catch (IOException e) { logger.error("加载配置文件异常", e); } }
发送短信
@Override public boolean send(String mobile, String msg) { logger.info(String.format("请求发送短信[%s, %s]", mobile, msg)); try { msg = URLEncoder.encode(msg, "UTF-8"); System.out.println("start to send sms"); String tmp =httpPost(smsUrl, "id="+smsAccount+"&psw="+smsPassword+"&mobile="+mobile+"&msg="+msg); System.out.println(tmp); if(tmp!=null&&tmp.contains("\"msg\": \"Success\"")){ System.out.println("success to send sms"); return true; } } catch (UnsupportedEncodingException e) { System.out.println("fail to send sms"); e.printStackTrace(); } return false; }
根据返回结果,匹配
返回值
错误描述
0
成功
1
提交参数不能为空
2
账号无效,
3
账号密码错误,
4
时间格式不正确,格式为:yyyy-MM-dd HH:mm:ss
20
系统错误