短信功能技术文档

265 阅读1分钟

第三方地址

该技术是中国网建的接口。电话:400-699-1455
官方网址:www.smschinese.cn/Rates.shtml
官方qq:4006991455

使用前准备:

1、需要注册一个账号
2、注册成功以后,去注册网页中查看秘钥

代码接口:

package com.tfkj.mavenfireoa12.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class SendNote {

/**
* @param account 注册账户 "futiankeji999"
* @param messageKey 去注册网页中查看秘钥 "d41d8cd98f00b204e980"
* @param phone 需要发送给某一个人的电话号 "18325466027"
* @param messageContent 发送的文本内容 "通知:今天请吃饭"
* @return
* @throws Exception
*/

    public static int sendMessage(String phone, String messageContent) throws Exception {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod("http://gbk.api.smschinese.cn");
        post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码
        String account = "futankeji999";
        String messageKey = "d41d8cd98f00b204e980";
        NameValuePair[] data ={ new NameValuePair("Uid", account),new NameValuePair("Key", messageKey),new NameValuePair("smsMob",phone),new NameValuePair("smsText",messageContent)};

        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();
        int flag = Integer.parseInt(result);
        return flag;
    }
}

pom依赖:

<!-- 短信发送 -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.4</version>
</dependency>

<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>