java手机发送短信验证码

2019-11-07 16:56:39 浏览数 (1)

找到一个短信验证平台:这里用的是:中国网建SMS短信平台。

网址:http://sms.webchinese.cn/api.shtml

导入下列包

测试代码:

代码语言:javascript复制
import java.io.IOException;

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 duanxin {
	
	public static void main(String[] args) throws HttpException, 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", "橙子123"),//用户名
				new NameValuePair("Key", "9345b399b*****"),//密码
				new NameValuePair("smsMob","131******"),//电话号码
				new NameValuePair("smsText","您的验证码为:8888")};//短信内容
		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();

	}
}

点击运行就可以了。

短信是收费的。

0 人点赞