java实现发送短信

本程序是经过使用中国网建提供的SMS短信平台实现的(该平台目前为注册用户提供5条免费短信,3条免费彩信,这足够用于咱们测试用了。在使用前须要注册,注册地址为http://sms.webchinese.cn/reg.shtmlhtml

下面是实现发送短信的java源码:java

package com.weixinsf.utils;

/**
 * <p>Title: 短信发送 </p>
 * 
 * <p>Description: 发送短信的工具类 </p>
 * 
 * @author liufeng
 * @date 2016-9-5
 * @version 1.0
 */
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", "用户名"),//中国网建sms平台注册的用户名
                new NameValuePair("Key", "用户密钥"),//中国网建sms平台注册的用户密钥
                new NameValuePair("smsMob", "13888888888"),//将要发送到的手机号码
                new NameValuePair("smsText", "验证码:280934") };//要发送的短信内容
        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包web

相关文章
相关标签/搜索