重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这个首先你要确定一下短信平台,他们会给你提供短信实现的接口文档。
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了白云免费建站欢迎大家使用!
比如:
public static String doPost(String reqUrl, Map parameters, String recvEncoding)
{
HttpURLConnection url_con = null;
String responseContent = null;
try
{
StringBuffer params = new StringBuffer();
Iterator iter = parameters.entrySet().iterator();
while (iter
.hasNext())
{
Map.Entry element = (Map.Entry)iter.next();
params.append(element.getKey().toString());
params.append("=");
params.append(URLEncoder.encode(element.getValue().toString(),
requestEncoding));
params.append("");
}
if (params.length() 0)
{
params = params.deleteCharAt(params.length() - 1);
}
URL url = new URL(reqUrl);
url_con = (HttpURLConnection)url.openConnection();
url_con.setRequestMethod("POST");
System.setProperty("sun.net.client.defaultConnectTimeout",
String.valueOf(connectTimeOut));
System.setProperty("sun.net.client.defaultReadTimeout",
String.valueOf(readTimeOut));
url_con.setDoOutput(true);
byte[] b = params.toString().getBytes();
url_con.getOutputStream().write(b, 0, b.length);
url_con.getOutputStream().flush();
url_con.getOutputStream().close();
InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(
new InputStreamReader(in,
recvEncoding));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
String crlf = System.getProperty("line.separator");
while (tempLine != null)
{
tempStr.append(tempLine);
tempStr.append(crlf);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
rd.close();
in.close();
}
catch (IOException localIOException)
{
}
finally
{
if (url_con != null)
{
url_con.disconnect();
}
}
return responseContent;
}
public static String sendTelCode(String mobile,String telcode){
MapString ,String map = new HashMapString ,String();
map.put("account", "Babo");
map.put("mobile", mobile);
map.put("pswd", "D3dddD");
try {
map.put("msg", java.net.URLEncoder.encode("您的验证码是"+telcode+",若非本人操作请忽略","utf-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String getValue = doPost("", map, "UTF-8");
System.out.println(getValue);
return getValue;
}
那你要做的就是向这个URL发送数据就好,给个例子:
public class Httptest1 {
8
9 public static void main(String[] args) {
10
11 URL url = null ;
12 HttpURLConnection conn = null ;
13 OutputStream outStream = null ;
14 InputStream inStream = null ;
15
16 try {
17 url = new URL( " " );
18 conn = (HttpURLConnection) url.openConnection();
19 conn.setDoOutput( true );
20 conn.setDoInput( true );
21
22 String sendXml = " ?xml version=\ " 1.0 \ " encoding=\ " UTF - 16 \ " ?!DOCTYPE SigMailCommand SYSTEM \ " SigMail.dtd\ " " ; // XML数据
23 sendXml += " SigMailCommand Name=\ " checkuser\ " Param Name=\ " domainname\ " test.com.cn/Param " ;
24 sendXml += " Param Name=\ " username\ " admin/Param/SigMailCommand " ;
25
26 outStream = conn.getOutputStream();
27
28 // 准备通过CONN对象写入XML数据
29 BufferedWriter bw = new BufferedWriter( new java.io.OutputStreamWriter(outStream,
30 " UTF-16 " ));
31 bw.write(sendXml);
32 bw.flush();
33 bw.close();
34
35 // DataOutputStream dataOutStream = new DataOutputStream(outStream);
36 // dataOutStream.writeChars(xml);
37 // dataOutStream.flush();
38 // dataOutStream.close();
39
40
41 // 准备通过CONN对象读取返回的XML数据
42
43 inStream = conn.getInputStream();
44 StringBuffer returnXml = new StringBuffer( "" );
45 BufferedReader rd = new BufferedReader( new InputStreamReader(inStream,
46 " UTF-16 " ));
47 for (String line = null ; (line = rd.readLine()) != null ;) {
48 returnXml.append(line);
49 }
50 System.out.println(returnXml.toString());
51 rd.close();
52 } catch (IOException ex) {
53 ex.printStackTrace();
54 } finally {
55 try {
56 if (outStream != null )
57 outStream.close();
58 if (inStream != null )
59 inStream.close();
60 if (conn != null )
61 conn.disconnect();
62 } catch (IOException e) {
63 // TODO 自动生成 catch 块
64 e.printStackTrace();
65 }
66 }
67
68 }
69
70 }
JAVA实现短信群发的步骤:
1、使用第三方短信平台服务商,接入短信服务;
2、调用短信提交页面发送请求;
3、服务器向第三方短信平台提交发送请求;
4、短信平台通过运营商将短信下发至用户的手机上。
以下是秒赛短信平台JAVA短信验证码接口代码示例
package test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang3.StringUtils;
public class Apis {
// 短信发送接口的http地址,请咨询客服
private static String url = “xxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
// 编码格式。发送编码格式统一用UTF-8
private static String ENCODING = “UTF-8”;
public static void main(String[] args) throws IOException, URISyntaxException {
// 账号
String account = “************************”;
// 密码
String pswd = “************************”;
// 修改为您要发送的手机号,多个用,分割
String mobile = “13*********”;
// 设置您要发送的内容
String msg = “【秒赛科技】您的验证码是:1234”;
// 发短信调用示例
System.out.println(Apis.send(account,pswd, mobile, msg));
}
/**
* 发送短信
*
* @param account
* account
* @param pswd
* pswd
* @param mobile
* 手机号码
* @param content
* 短信发送内容
*/
public static String send(String account,String pswd, String mobile, String msg) {
NameValuePair[] data = { new NameValuePair(“account”, account),
new NameValuePair(“pswd”, pswd),
new NameValuePair(“mobile”, mobile),
new NameValuePair(“msg”, msg),
new NameValuePair(“needstatus”, “true”),
new NameValuePair(“product”, “”) };
return doPost(url, data);
}
/**
* 基于HttpClient的post函数
* PH
* @param url
* 提交的URL
*
* @param data
* 提交NameValuePair参数
* @return 提交响应
*/
private static String doPost(String url, NameValuePair[] data) {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
// method.setRequestHeader(“ContentType”,
// “application/x-www-form-urlencoded;charset=UTF-8”);
method.setRequestBody(data);
// client.getParams()。setContentCharset(“UTF-8”);
client.getParams()。setConnectionManagerTimeout(10000);
try {
client.executeMethod(method);
return method.getResponseBodyAsString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}