java短信验证接口_java代码实现调用短信接口,发送短信验证。

packagecom.aaa.zxf.login;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.NameValuePair;importorg.apache.commons.httpclient.methods.PostMethod;importorg.dom4j.Document;importorg.dom4j.DocumentHelper;importorg.dom4j.Element;/*** 一、登录这个网站,注册登录。http://www.ihuyi.com/product.php可以免费使用10次。

*

*

*二、找到他的代码示例。在下面会有图片说明

*

*

*三、根据自己的需要,可以对示例代码进行修改。下面的代码是来自于对https://blog.csdn.net/qq_17025903/article/details/73331091的编辑。

*

*

*四、jar包管理。

* 1. 点击他的接口下载,里面提供的有jar包。

* 2.maven项目需要直接去搜索

*

*

*

*

* commons-codec

* commons-codec

* 1.3

*

*

*

*

*

* dom4j

* dom4j

* 1.6.1

*

*

*

*

*

*

* org.apache.httpcomponents

* httpclient

* 4.5.6

*

*

*

*

* commons-httpclient

* commons-httpclient

* 3.0-rc4 我的这个jar包导不进来,maven项目没有搞定。

*

*

**/

public classSendsmsDemo {private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit"; //直接将示例代码中的URL 拿过来用就行。

public static intsendSms(String phone) {

HttpClient client= newHttpClient();

PostMethod method= newPostMethod(Url);

client.getParams().setContentCharset("GBK");

method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=GBK");int mobile_code = (int) ((Math.random() * 9 + 1) * 100000);

String content= new String("您的验证码是:" + mobile_code + "。请不要把验证码泄露给其他人。");//提交短信

NameValuePair[] data ={//name 和 password 需要自行修改。

new NameValuePair("account", "C51843367"), //查看用户名 是登录用户中心->验证码短信->产品总览->APIID

new NameValuePair("password", "4839f1a37f6e5240109e6bf1cc8fd647"), //查看密码请登录用户中心->验证码短信->产品总览->APIKEY

new NameValuePair("mobile", phone),new NameValuePair("content", content),

};

method.setRequestBody(data);try{

client.executeMethod(method);

String SubmitResult=method.getResponseBodyAsString();

System.out.println(SubmitResult);

Document doc=DocumentHelper.parseText(SubmitResult);

Element root=doc.getRootElement();

String 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 ("2".equals(code)) {

System.out.println("短信提交成功");return mobile_code; //验证码

} else return 0;

}catch(Exception e) {

e.printStackTrace();return 0;

}

}//测试 手机号

public static voidmain(String[] args) {

sendSms("15290859821");

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值