准备工作
登陆网站 :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
您可能感兴趣的文章:
- 样式加载不出来,浏览器控制台报错:Resource interpreted as Stylesheet but transferred with MIME type text/html
- servlet之ServletContext简介_动力节点Java学院整理
- jsp 页面显示的一些用法
- springMVC常见错误-解决org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.spring
- 快速排序 java详解
- SpringBoot使用JSP作为视图模板的方法
如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!
网友评论