java通过sms发送短信

来源:互联网 发布:宇宙射线知乎 编辑:程序博客网 时间:2024/05/16 06:36

需要的jar包

commons-logging-1.1.1.jar
commons-httpclient-3.1.jar
commons-codec-1.4.jar

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");//在头文件中设置转码//这里的参数在sms(中国建网)api那里有NameValuePair[] data ={ new NameValuePair("Uid", "sms注册的用户账号"),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();}

成功显示1

原创粉丝点击