JAVA如何实现发送短信
生活随笔
收集整理的這篇文章主要介紹了
JAVA如何实现发送短信
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天看到了一個需求,需要做一個發送短信的功能,網上找了找,大概有這幾種方法:(1)使用webservice接口發送手機短信,這個可以使用sina提供的webservice進行發送,但是需要進行注冊;(2)使用短信mao的方式進行短信的發送,這種方式應該是比較的常用,前提是需要購買硬件設備,呵呵(3)使用中國網建提供的SMS短信平臺(申請賬號地址:http://sms.webchinese.com.cn/),聽說了網建提供了幾條免費的短信,就上去試了試,調用了下他的接口(實為調用接口,其實就是拷貝了一段代碼,呵呵~),代碼如下,和大家分享一下:
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", "本站用戶名"),new NameValuePair("Key", "接口安全秘鑰"),new NameValuePair("smsMob", "手機號碼"),new NameValuePair("smsText", "驗證碼:8888") };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.jar
commons-logging-1.1.1.jar
總結
以上是生活随笔為你收集整理的JAVA如何实现发送短信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery autocomplete
- 下一篇: 解决nginx负载均衡的session共