1. Introduction of Interface Usage
Sending SMS is sure to use a third-party interface, and Java itself is definitely not able to send text messages directly. There are a lot of third-party interface, here directly to find a regular and reliable a little learning
China Network Construction (http://sms.webchinese.cn/) is used here.
After opening the URL, click Register now
2. Get the SMS Key
Once the registration is complete, check the red SMS key. Click Edit SMS Key
here is the key to remember the message, of course, you can also modify the SMS key
3. Using SMS API Interface
Click on the SMS API interface on the left, or enter http://sms.webchinese.cn/api.shtml directly
The following interface appears:
Find the Java sample program below
4. Java code
12345678910111213141516171819202122232425262728293031323334353637383940 |
package com.fz.http;
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;
/**
* 类说明 :利用中国网建接口发送短信
* @author FangZheng
* @version 时间:2015-8-30 下午6:21:10
* 1、打开:http://sms.webchinese.cn/ 注册一个用户,默认用户是5条短信
* 2、打开api短信接口,找到修改短信秘钥菜单,找到自己的短信秘钥
* 3、使用api接口来发送短信,官方给的有java,php等各种语言的例子即可发送短信
*/
public class SendMsgTest {
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"
,
"zhangsan"
),
new NameValuePair(
"Key"
,
"23825bbfbd73c539f05e"
),
new NameValuePair(
"smsMob"
,
"1307*******"
),
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();
}
}
|
The Java program runs on 3 jar packages, which can be downloaded directly on its page.
The final program ran successfully, and the corresponding mobile phone number also received a text message.
From for notes (Wiz)
Java Send SMS