String result= "";//返回状态 Note note = new Note(); String Tel = "17089490559";//接收人手机号 String message = "您好!";//短信内容 result = note.sendNote(Tel,message);//信息发送状态
id=xxx(xxx是你注册的用户名) key=********(注册成功后平台提供的短信密钥)
public class ConfigNoteInfo {
private static ConfigNoteInfo configNoteInfo;
private static Properties properties;
private ConfigNoteInfo(){
//note.properties是你在根目录新建的配置文件
String configFile="note.properties";
properties=new Properties();
InputStream in=ConfigNoteInfo.class.getClassLoader().getResourceAsStream(configFile);
try {
properties.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static ConfigNoteInfo getInstance(){
if(configNoteInfo==null){
configNoteInfo = new ConfigNoteInfo();
}
return configNoteInfo;
}
public String getString(String key){
return properties.getProperty(key);
}
}
package cn.hs.tools;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;
/***
* @author HeShuang
*@Create_date:2017-5-4
*/
public class Note {
//短信接口,传参(手机号,短信内容)即可发送短信。
public String sendNode(String Tel,String message) throws HttpException, IOException{
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");// 在头文件中设置转码,这里为UTF-8
NameValuePair[] data = {
new NameValuePair("Uid", ConfigNoteInfo.getInstance().getString("id")), // 注册的用户名,我们通过单例模式从配置文件id中读取用户名。
new NameValuePair("Key",ConfigNoteInfo.getInstance().getString("key")), // 注册成功后,登录网站后得到的密钥 ,同样从配置文件读取。
new NameValuePair("smsMob", Tel), // 传递过来的手机号码
new NameValuePair("smsText", message) // 传递过来的短信内容
};
post.setRequestBody(data);
client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
int flag = post.getStatusCode(); //返回状态,参照API
System.out.println("statusCode:" + flag);
for (Header h : headers) {
System.out.println("---" + h.toString());
}
String result = new String(post.getResponseBodyAsString().getBytes(
"UTF-8"));
System.out.println(result);
String info="";
if(Integer.parseInt(result.trim())>0){
info="发送成功!";
}
switch (result.trim()) {
case "-1":
info="用户账号不存在!";
break;
case "-2":
info="接口密钥不正确";
break;
case "-3":
info="尊敬的用户,卖血卖肾,请您先把短信费用缴了!";
break;
case "-4":
info="手机号格式不正确!";
break;
case "-6":
info="IP存在限制!";
break;
case "-11":
info="该用户已被禁用!";
break;
case "-14":
info="短信内容存在非法字符!";
break;
default:
break;
}
return info;
}
}
String result= "";//返回状态 Note note = new Note(); String Tel = "17089490559";//要发送的手机号 String message = "您好!";//要发送的短信信息 result = note.sendNote();//获取信息发送状态
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有