java发送手机短信demo

JAVA发送手机短信有几种方法:

(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;

(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备;

(3)使用中国网建提供的SMS短信平台(申请账号地址:http://sms.webchinese.cn/default.shtml)

本程序运行需要导入3个jar文件,点击下面链接下载即可

commons-logging-1.1.1.jar

commons-httpclient-3.1.jar

commons-codec-1.4.jar

java代码,仅供参考:

public static String sendMsg(String...str) 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",str[0] ),  
                new NameValuePair("Key", str[1]),  
                new NameValuePair("smsMob", str[2]),  
                new NameValuePair("smsText", str[3]) };  
        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(); 
        return result;
	
	}

	public static void main(String[] args) throws Exception {
		String [] str = new String[]{"guardian", "6185dd*******8b6bd7a", "153******06" ,"测试短信接收:手机验证码:12451597474"};
		sendMsg(str);
	}

友情提示:

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=接口安全密码

短信发送后返回值

  说 明

-1

没有该用户账户
-2 接口密钥不正确 [查看密钥]
不是账户登陆密码
-21 MD5接口密钥加密不正确
-3 短信数量不足
-11 该用户被禁用
-14 短信内容出现非法字符
-4 手机号格式不正确
-41 手机号码为空
-42 短信内容为空
-51 短信签名格式不正确
接口签名格式为:【签名内容】
-6 IP限制
大于0 短信发送数量

猜你喜欢

转载自blog.csdn.net/hekaihaw/article/details/53787046