码迷,mamicode.com
首页 > 移动开发 > 详细

JAVA发送手机短信

时间:2017-05-27 21:18:45      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:html   发送   str   java程序   cat   .com   ase   实现   cli   

<p><span>JAVA发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备,呵呵;</span></p>

 

 

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://sms.webchinese.cn/web_api/"); 
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", "java程序发的信息!!") }; 
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(); 

运行上面代码就可以实现发送短信的功能

 

短信的sms的官网:http://sms.webchinese.com.cn/api.shtml

JAVA发送手机短信

标签:html   发送   str   java程序   cat   .com   ase   实现   cli   

原文地址:http://www.cnblogs.com/mybloging/p/6914356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!