这篇文章给大家分享的是有关JAVA如何实现第三方短信发送过程的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
想使代码生效需要注册:
http://sms.webchinese.cn/default.shtmlhttp://sms.webchinese.cn/default.shtml
在muven项目里面导入jar包
<dependencies>
<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.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.8</version>
</dependency>
</dependencies>
创建一个测试类:下面代码粘贴上去用就行了
public static void main(String[] args) throws HttpException, IOException {
// TODO Auto-generated method stub
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", "****"), // 注册的用户名
new NameValuePair("Key", "********"), // 这个key需要在里面获取
new NameValuePair("smsMob", "*********"), // 手机号码
new NameValuePair("smsText", "嘻嘻!猜猜我是谁?") };//设置短信内容
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();
}
输出结果成功后是这个效果;这个值代表短信发送的数量如果下面的返回值不是1请参照以下图片
可能需要的的问题:
当然 返回值等于1的时候不代表你就能收到短信 只能说明你短信发送成功了想要短信成功的接收不需要进去官网去申请短信接收接口
感谢各位的阅读!关于“JAVA如何实现第三方短信发送过程”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。