Java代码实现手机发送短信功能

准备工作

登陆网站 :SMS短信通-注册,注册一个账号。 

http://sms.webchinese.cn/api.shtml#top3

Java代码实现手机发送短信功能_第1张图片之后看密钥

Java代码实现手机发送短信功能_第2张图片Java代码实现手机发送短信功能_第3张图片下载jar包

Java代码实现手机发送短信功能_第4张图片

import java.io.UnsupportedEncodingException;
import java.util.Random;

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.api.smschinese.cn");
		post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//在头文件中设置转码
		NameValuePair[] data ={ new NameValuePair("Uid", "zhuye"),new NameValuePair("Key", "d41d8cd98f00b204e980"),new NameValuePair("smsMob","13704265183"),new NameValuePair("smsText","验证码:"+createRandomVcode())};
		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();

}
public static String createRandomVcode(){
	Random random = new Random();
    //验证码
    String vcode = "";
    for (int i = 0; i < 6; i++) {
        vcode = vcode + (int)(random.nextInt(10));
    }
    return vcode;
}
}

你可能感兴趣的:(Java代码实现手机发送短信功能)