项目中,在用户注册时,有时需要用户填写手机号码,并发送短信验证码,今天就说下该功能的实现
该功能的重点就是调用短信平台接口,模拟Http请求
具体步骤如下:
1、导入所需jar包
commons-codec-1.10.jar
commons-logging-1.1.3.jar
httpclient-4.5.2.jar
2、模拟http发送请求
本文使用的短信平台接口是中国网建SMS短信通http://www.smschinese.cn/api.shtml
HttpClient client = new HttpClient();PostMethod post = new PostMethod("http://utf8.sms.webchinese.cn");post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");NameValuePair[] data = {new NameValuePair("Uid", "用户名"),new NameValuePair("Key", "接口密钥"),new NameValuePair("smsMob","手机号码"),new NameValuePair("smsText","验证码:8888")};post.setRequestBody(data);System.out.println("验证码发送成功!"); try {client.executeMethod(post);//获取返回数据System.out.println("状态码::"+post.getStatusCode());post.releaseConnection();} catch (Exception e) {e.printStackTrace();}
让我们一起遨游在代码的海洋里!
本文链接:https://my.lmcjl.com/post/20187.html
展开阅读全文
4 评论