java短信发送

使用java发送短信最简单的方法就是在

http://sms.webchinese.cn/api.shtml注册帐号

然后就可以根据此站提供的接口发送了。

里面提供了多种语言的发送方法,什么东向西都写的很清楚。

jar包下载
commons-logging-1.1.1.jar
commons-httpclient-3.1.jar
commons-codec-1.4.jar

 1 JAVA调用
 2 
 3 import java.io.UnsupportedEncodingException;
 4  import org.apache.commons.httpclient.Header;
 5  import org.apache.commons.httpclient.HttpClient;
 6  import org.apache.commons.httpclient.NameValuePair;
 7  import org.apache.commons.httpclient.methods.PostMethod;
 8 
 9 public class SendMsg_webchinese {
10 
11  public static void main(String[] args)throws Exception
12  {
13 
14  HttpClient client = new HttpClient();
15  PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn"); 
16  post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码
17  NameValuePair[] data ={ new NameValuePair("Uid", "本站用户名"),new NameValuePair("Key", "接口安全密码"),new NameValuePair("smsMob","手机号码"),new NameValuePair("smsText","短信内容")};
18  post.setRequestBody(data);
19 
20  client.executeMethod(post);
21  Header[] headers = post.getResponseHeaders();
22  int statusCode = post.getStatusCode();
23  System.out.println("statusCode:"+statusCode);
24  for(Header h : headers)
25  {
26  System.out.println(h.toString());
27  }
28  String result = new String(post.getResponseBodyAsString().getBytes("gbk")); 
29  System.out.println(result);
30 
31 
32  post.releaseConnection();
33 
34  }
35 
36 }

短信发送后返回值:
-1 没有该用户账户
-2 密钥不正确 [查看密钥]
-3 短信数量不足
-11 该用户被禁用
-14 短信内容出现非法字符
-4 手机号格式不正确
-41 手机号码为空
-42 短信内容为空
大于0 短信发送数量

 

 

 

具体的推荐自己去看看

posted @ 2013-06-08 18:41  果c子  阅读(287)  评论(0编辑  收藏  举报