发送短信实例

本文介绍了一种使用MD5加密登录密码的方法,并通过XML格式拼接短信接口所需的参数,实现了短信发送功能。同时,提供了参数校验逻辑及HTTP请求调用短信服务的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.sino.java.thirdparty;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.sino.java.utility.Http;

public class VcomUtil {

    /**
     * 将登录密码采用32位大写MD5方式加密
     * @return
     * @throws Exception
     */
    public static String Md5Pwd(){
        
         StringBuffer buf = new StringBuffer();
         MessageDigest md;
         String pwd = null;
        try {
            md = MessageDigest.getInstance("MD5");
             md.update("xntx123".getBytes());
             byte b[] = md.digest();
             int i;
             for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                  if (i < 0) {
                    i += 256;
                  }
                  if (i < 16) {
                    buf.append("0");
                  }
                  buf.append(Integer.toHexString(i));
              }
            pwd = buf.toString().toUpperCase();
            
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        return pwd;
    }
    
    /**
     * 拼接短信接口XML
     * @param OpKind
     * @param InterFaceID
     * @param SerType
     * @param ETime
     * @param RecivePhoneNum
     * @param Content
     * @param SearchID
     * @param num
     * @return
     * @throws Exception
     */
    public static  String sendNumAndContent(String OpKind,String InterFaceID,
            String SerType,String ETime,String[] RecivePhoneNum,String[] Content,String[] SearchID,int num) throws Exception{
        
        StringBuffer buffer = new StringBuffer();
        
        buffer.append("<Group Login_Name='xntx' Login_Pwd='"+Md5Pwd()+"' OpKind='"+OpKind+"' InterFaceID='"+InterFaceID+"'");
        if(SerType != null && !SerType.equals("")){
                
            buffer.append("SerType='"+SerType+"'");
        }
        buffer.append(">");
        if(ETime != null && !ETime.equals("")){
            
            buffer.append("<E_Time>"+ETime+"</E_Time>");
        }else{
            buffer.append("<E_Time></E_Time>");
        }
        if(OpKind != "0" && !OpKind.equals("0")){
            buffer.append("<Mobile>");
            for(int i=0;i<num;i++){
                buffer.append(RecivePhoneNum[i]);
                if(i != num-1){
                    buffer.append(",");
                }
            }
            
            buffer.append("</Mobile>");
            buffer.append("<Content><![CDATA["+Content[0]+"]]></Content>");
            buffer.append("<ClientID>"+SearchID[0]+"</ClientID>");
        }
        else {
            
            buffer.append("<Item>");
            for(int i=0;i<num;i++){
                buffer.append("<Task>");
                buffer.append("<Recive_Phone_Number>"+RecivePhoneNum[i]+"</Recive_Phone_Number>");
                buffer.append("<Content><![CDATA["+Content[i]+"]]></Content>");
                buffer.append("<Search_ID>"+SearchID[i]+"</Search_ID>");
                buffer.append("</Task>");
            }
            buffer.append("</Item>");
            
        }
        buffer.append("</Group>");
        
//        System.out.println("--------------------------"+buffer);
        
        
        return buffer.toString();
    }
    
    /**
     *判断传入的参数的正确性
     * @param OpKind
     * @param InterFaceID
     * @param SerType
     * @param ETime
     * @param RecivePhoneNum
     * @param Content
     * @param SearchID
     * @param num
     * @return
     */
    public static boolean verificateSend(String OpKind,String InterFaceID,
            String SerType,String ETime,String[] RecivePhoneNum,String[] Content,String[] SearchID,int num){
        boolean flag=false;
        String str=null;
        if(SerType.length() > 5){
            str="SerType不能超过5长度的字符串";
            System.out.println(str);
            return flag;
        }
        if(OpKind == null || OpKind.equals("")){
            str="业务类型不能为空,默认为0";
            System.out.println(str);
            return flag;
        }
        else if(OpKind.equals("51")){
                if(RecivePhoneNum.length > 50){
                    str= "手机号的个数不能超过50个";
                    System.out.println(str);
                    return flag;
                }
        }
        else if(OpKind.equals("0")){
                if(RecivePhoneNum.length > 10){
                    str= "手机号的个数不能超过10个";
                    System.out.println(str);
                    return flag;
                }
            
        }
        else{
            str= "OpKind 值无效";
            System.out.println(str);
            return flag;
        }
        
        for(int i=0;i<num;i++){
            if(SearchID[i].length() > 40){
                System.out.println(str);
                str="流水号的长度不能超过40";
            }
        }
        
        
        
        return true;
    }
    
    /**
     * 调用短信接口
     * @param OpKind
     * @param InterFaceID
     * @param SerType
     * @param ETime
     * @param RecivePhoneNum
     * @param Content
     * @param SearchID
     * @param num
     * @return
     * @throws Exception
     */
    public static String send(String OpKind,String InterFaceID,
            String SerType,String ETime,String[] RecivePhoneNum,String[] Content,String[] SearchID,int num) throws Exception{
        
//        String html="";
//        System.out.println(verificateSend(OpKind, InterFaceID, SerType, ETime, RecivePhoneNum, Content, SearchID, num));
        if(verificateSend(OpKind, InterFaceID, SerType, ETime, RecivePhoneNum, Content, SearchID, num)){
            
            return Http.Send("POST","http://slapi.vcomcn.com/Opration.aspx",sendNumAndContent(OpKind, InterFaceID, SerType, ETime, RecivePhoneNum, Content, SearchID, num),"GB2312");
        }
        
            return "exception";
        
    }
    
    
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值