Java代码实现手机发送短信功能
程序员文章站
2022-06-30 19:33:21
准备工作登陆网站 :SMS短信通-注册,注册一个账号。 http://sms.webchinese.cn/api.shtml#top3之后看密钥下载jar包import java.io.UnsupportedEncodingException;import java.util.Random;import org.apache.commons.httpclient.Header;import org.apache.commons.httpclient.HttpClient;import...
准备工作
登陆网站 :SMS短信通-注册,注册一个账号。
http://sms.webchinese.cn/api.shtml#top3
之后看密钥
下载jar包
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;
}
}
本文地址:https://blog.csdn.net/xiaohaizi15/article/details/107373080