1. 程式人生 > >WebService短信網關配置

WebService短信網關配置

系統錯誤 導入 tab mss rom -s center border tour

第一步: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

系統錯誤

WebService短信網關配置