java发送短信
package sms;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class SendMsg_webchinese {
public static void main(String[] args)throws Exception{
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn");
post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码
NameValuePair[] data ={
new NameValuePair("Uid", "qxxxx"),
new NameValuePair("Key", "dcd082xxxxxd27057"),
new NameValuePair("smsMob","186xxxxx20"),
new NameValuePair("smsText","这个是内容 ")
};
post.setRequestBody(data);
client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
int statusCode = post.getStatusCode();
System.out.println("statusCode:"+statusCode);
for(Header h : headers){
System.out.println(h.toString());
}
String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
System.out.println(result);
post.releaseConnection();
}
}
commons-logging-1.1.1.jar
需要引入上面三个jar包
注册中国网建账号 官网地址
http://sms.webchinese.cn/
UID 是注册的账号
Key 是短信密钥(注:不是密码)在中国网建 上面可以查看到
smsmob 是接收方电话号码
Smstext 是短信内容
记得先增加短信签名 不然报错-51