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");
}
}