0
点赞
收藏
分享

微信扫一扫

java通过短信接口发送短信


  • 短信商 二  http://www.奇讯云.com/

     */
    //之前的private static String Url = "http://奇讯云.com/webservice/sms.php?method=Submit";
    
    private static String Url = "http://api.cnsms.cn/?ac=send";
    
    /**

  • 给一个人发送单条短信
  • @param mobile 手机号
  • @param code  短信内容

     */
    public static void sendSms2(String mobile,String code){
        HttpClient client = new HttpClient(); 
        PostMethod method = new PostMethod(Url); 
            
        client.getParams().setContentCharset("UTF-8");
        method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
        String content = new String(code);  
        
        String account = "", password = "";
        String strSMS2 = Tools.readTxtFile(Const.SMS2);            //读取短信2配置
        if(null != strSMS2 && !"".equals(strSMS2)){
            String strS2[] = strSMS2.split(",fh,");
            if(strS2.length == 2){
                account = strS2[0];
                password = strS2[1];
            }
        }
       
        NameValuePair[] data = {//提交短信
            new NameValuePair("uid", account), 
            new NameValuePair("pwd",  MD5.md5(password)),             //秘钥可以使用明文秘钥或使用32位MD5加密
            new NameValuePair("mobile", mobile), 
            new NameValuePair("content", content),
            new NameValuePair("encode", "utf8"),
        };
        
        method.setRequestBody(data);
        
        try {
            client.executeMethod(method);
            
            String SubmitResult =method.getResponseBodyAsString();
                    
            Document doc = DocumentHelper.parseText(SubmitResult); 
            Element root = doc.getRootElement();
            code = root.elementText("code");
            String msg = root.elementText("msg");
            String smsid = root.elementText("smsid");
            
            
            System.out.println(code);
            System.out.println(msg);
            System.out.println(smsid);
            
            if(code == "2"){
                System.out.println("短信提交成功");
            }
            
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }    
        
    }
    
    /**

  • 给多个人发送单条短信
  • @param list 手机号验证码

     */
    public static void sendSmsAll(List<PageData> list){
        String code;
        String mobile;
        for(int i=0;i<list.size();i++){
            code=list.get(i).get("code").toString();
            mobile=list.get(i).get("mobile").toString();
            sendSms1(mobile,code);
        }
    }

举报

相关推荐

0 条评论