java 实现简单的短信发送
Posted TheStar
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 实现简单的短信发送相关的知识,希望对你有一定的参考价值。
现在中国网建上注册一个自己的账户,
然后里面有代码案例,也有相应的下载jar包的地址
package com.direct.note; 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; public class Message { public static void main(String[] args) { try { new Message().sendMessage(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void sendMessage() throws HttpException, IOException{ HttpClient client = new HttpClient(); PostMethod post = new PostMethod("http://gbk.api.smschinese.cn"); //在头文件中设置转码 post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk"); int count = (int)(Math.random()*10000)+1; System.out.println(count); String message = "验证码:"+count; NameValuePair[] data = { new NameValuePair("Uid", "theStar"), new NameValuePair("Key", "你的key"),//73d1e45fd5f0fa2b9a87 new NameValuePair("smsMob", "发送的电话号码"), new NameValuePair("smsText", message) }; post.setRequestBody(data); client.executeMethod(post); Header[] headers = post.getRequestHeaders(); 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(); } }
以上是关于java 实现简单的短信发送的主要内容,如果未能解决你的问题,请参考以下文章