JAVA实现手机发送验证码

public class LinkSMS {
    public static String HttpClientTest(String mobile,String content) {
  		  // 接口账户名
        String CorpID = "";
        // 接口密码
        String Pwd = "";
        String result="";
        try {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            AuthCache authCache = new BasicAuthCache();
            HttpClientContext context = HttpClientContext.create();
            context.setAuthCache (authCache);
            //2.封装短信接口参数
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("CorpID",CorpID));
            params.add(new BasicNameValuePair("Pwd",Pwd));
            params.add(new BasicNameValuePair("Mobile", mobile));
            params.add(new BasicNameValuePair("Content", content));
            params.add(new BasicNameValuePair("Cell", ""));
            params.add(new BasicNameValuePair("SendTime", ""));
            HttpEntity entity = new UrlEncodedFormEntity(params,"GBK");
            //3.提交访问地址
            HttpPost request = new HttpPost ("https://sdk2.028lk.com");
            request.setEntity(entity);
            //4.提交访问短信接口
            CloseableHttpResponse response = httpclient.execute(request);
            //5.获取返回值并解析
            HttpEntity entity2 = response.getEntity();
            BufferedReader br = new BufferedReader(new InputStreamReader(entity2.getContent(), "GBK"));
            String line="";
            while ((line = br.readLine()) != null) {
                result+= line+"\n";
            }
            //4.关闭链接
            br.close();
            response.close ();
            httpclient.close ();
        } catch (Exception e) {
            e.getLocalizedMessage();
        }
        return result;
    }
    /**
     *main测试
     */
    public static void main(String[] args) {
        String content="您的验证码为:233112,有效时间5分钟";
        String phone="18797512522";

        String result=LinkSMS.HttpClientTest(phone,content);
        System.out.println("----------------接口提交返回值-------------------");
        System.out.println(phone);
        System.out.println(content);
        System.out.print(result);
        System.out.println("----------------------------------------------");
        //对返回值的判断。。。此处省略
        if(result!=null){
            System.out.println("正确");
        }else {
            System.out.println("错误");
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值