java發送短信

最近在做CRM的項目,先接觸到的是發送短信。我是通過SMS平臺來發送短信的,本人還是菜鳥,如果有說的不對的還望大家給予指正,先謝謝了。

1.先到短信平臺去註冊用戶

2.註冊成功後,到接口API下找到UID和KEY,可以進行修改。

    GBK編碼發送接口地址
    http://gbk.sms.webchinese.cn/?Uid=本站用戶名&Key=接口安全密碼&smsMob=手機號碼&smsText=短信內容 
    UTF-8編碼發送接口地址:
    http://utf8.sms.webchinese.cn/?Uid=本站用戶名&Key=接口安全密碼&smsMob=手機號碼&smsText=短信內容
    獲取短信數量接口地址(UTF8):
    http://sms.webchinese.cn/web_api/SMS/?Action=SMS_Num&Uid=本站用戶名&Key=接口安全密碼
    獲取短信數量接口地址(GBK):
    http://sms.webchinese.cn/web_api/SMS/GBK/?Action=SMS_Num&Uid=本站用戶名&Key=接口安全密碼

 短信調用後會有相應的返回值,可以到SMS官網去查看返回值對應的意思。

3.來看看我寫的發送短信的工具類

package com.*.*.bsm.utils;


import com.*.*.sys.exception.SysException;

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;


import java.io.IOException;


/**

 * Created with IntelliJ IDEA.

 * User: Administrator

 * Date: 14-11-26

 * Time: 下午2:38

 * To change this template use File | Settings | File Templates.

 */

public class SendMsg {

    public static String sendMsg(String toUser,String content) throws IOException{

        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", "你的UID"),new NameValuePair("Key", "你的KEY"),new NameValuePair("smsMob",toUser),new NameValuePair("smsText",content)};

        post.setRequestBody(data);

        client.executeMethod(post);

        Header[] headers = post.getResponseHeaders();

        int statusCode = post.getStatusCode();

        for(Header h : headers)

        {

            System.out.println(h.toString());

        }

        String result = new String(post.getResponseBodyAsString().getBytes("gbk"));

        System.out.println(result); //打印返回消息狀態

        post.releaseConnection();

        return result;

    }

}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章