发送短信平台有很多,我们这里选择网建短信通。
1、操作步骤
1.1 准备工作
- 在http://www.webchinese.com.cn注册用户
- 设置签名
- 获取短信秘钥
- 接口分析
*参数说明
参数说明- 响应分析
1.2 完成java代码
- 创建一个maven普通项目
- 加入依赖
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0.1</version>
</dependency>
- 创建测试类以及测试main方法
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 Test {
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", "lizhangf"),
new NameValuePair("Key", "d41d8cd98f00b204e980"),
new NameValuePair("smsMob", "18951988367"),
new NameValuePair("smsText", "I love you")};
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();
}
}
- 执行main方法,完成发短信测试。
网友评论