【JAVA基础】0013--------java实现的一个发送手机短信的小例子

来源:互联网 发布:隐藏文件恢复软件 编辑:程序博客网 时间:2024/05/29 19:50

本程序主要是运用了中国网建提供的SMS短信平台,这个短信平台基于java提供个专门的接口


public class SMS {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", "爱新觉******");//注册的用户名new NameValuePair("Key", "*************");//注册成功之后,登陆网站时需要的秘钥new NameValuePair("smsMob","************");//要发送的目标手机号码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();}}

需要用到的JAR包:

commons-codec-1.4

commons-httpclient-3.1

commons-logging-1.1.1


原创粉丝点击