主要为大家分享java短信接口代码,java短信发送、java批量发送、java短信验证码发送,感兴趣的小伙伴们可以参考一下。
package com.util;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
/**
* author: cjianquan
* date: 2016/9/29
*/
public class ZxHttpUtil {
// 表示服务器端的url
private ZxHttpUtil() {
// TODO Auto-generated constructor stub
}
/*
* params 填写的URL的参数 encode 字节编码
*/
public static String sendPostMessage(String strUrl,Map params,
String encode) {
System.out.println(strUrl);
URL url = null;
try {
url = new URL(strUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
StringBuffer stringBuffer = new StringBuffer();
if (params != null && !params.isEmpty()) {
System.out.println("ddd");
for (Map.Entry entry : params.entrySet()) {
try {
stringBuffer
.append(entry.getKey())
.append("=")
.append(URLEncoder.encode(entry.getValue(), encode))
.append("&");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 删掉最后一个 & 字符
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
System.out.println("-->>" + stringBuffer.toString());
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setDoInput(true);// 从服务器获取数据
httpURLConnection.setDoOutput(true);// 向服务器写入数据
// 获得上传信息的字节大小及长度
byte[] mydata = stringBuffer.toString().getBytes();
// 设置请求体的类型
httpURLConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Lenth",
String.valueOf(mydata.length));
// 获得输出流,向服务器输出数据
OutputStream outputStream = (OutputStream) httpURLConnection
.getOutputStream();
outputStream.write(mydata);
// 获得服务器响应的结果和状态码
int responseCode = httpURLConnection.getResponseCode();
System.out.println(responseCode);
if (responseCode == 200) {
// 获得输入流,从服务器端获得数据
InputStream inputStream = (InputStream) httpURLConnection
.getInputStream();
return (changeInputStream(inputStream, encode));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("eee");
return "";
}
/*
* // 把从输入流InputStream按指定编码格式encode变成字符串String
*/
public static String changeInputStream(InputStream inputStream,
String encode) {
// ByteArrayOutputStream 一般叫做内存流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
String result = "";
if (inputStream != null) {
try {
while ((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
result = new String(byteArrayOutputStream.toByteArray(), encode);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
}
package com.util;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.google.gson.JsonObject;
public class Utility {
public static void jhson() {
String jsonString ="{\"name\":\"zhangsan\",\"password\":\"zhangsan123\",\"email\":\"10371443@qq.com\"}";
JsonObject json = new JsonObject();
}
public static String getNowDateStr() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String dateString = formatter.format(currentTime);
return dateString;
}
public static String getMD5(String str) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(str.getBytes("utf-8"));
System.out.println(toHex(bytes).toLowerCase());
return toHex(bytes).toLowerCase();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String toHex(byte[] bytes) {
final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
StringBuilder ret = new StringBuilder(bytes.length * 2);
for (int i=0; i
ret.append(HEX_DIGITS[(bytes[i] >> 4) & 0x0f]);
ret.append(HEX_DIGITS[bytes[i] & 0x0f]);
}
return ret.toString();
}
}
pageEncoding="utf-8"%>
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
SendSms// 您的短信账号
String Account = "您的短信账号";
// 您的短信账号密码
String Password = "您的短信账号密码";
// 是否需要状态报告
String NeedStatus = "true";
String message = "短信内容";//短信内容
String mobile = "xxxxxxxxxxx";//要发送的手机号,多个手机号用,隔开
String ts = Utility.getNowDateStr();//时间戳
Password = Utility.getMD5(Account + Password + ts);// Md5签名(账号+密码+时间戳)
HashMap params = new HashMap();
//请求参数
params.put("account",Account);
params.put("pswd",Password);
params.put("mobile",mobile);
params.put("msg",message);
params.put("ts",ts);
params.put("needstatus",NeedStatus);
String rep = ZxHttpUtil.sendPostMessage("http://139.196.108.241:8080/Api/HttpSendSMYzm.ashx", params, "UTF-8");
out.print(rep);
JsonParser parser = new JsonParser();
JsonObject json = (JsonObject)parser.parse(rep);
out.print("
result_msg:" + json.get("result_msg"));
%>