import java.io.IOException;
import java.io.UnsupportedEncodingException;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/**
* 利用的邮件、短信网关发送系统提示信息,发送系统信息给用户
*
* @author zhang
*
*/
public class SendMsgUtil {
// 电子邮件发送网关接口
public static final String NETGATE_EMAIL_URL = "http://xxx.xxxx.com.cn:8090/api/sendemail.php";
// 短信发送网关接口
public static final String NETGATE_SMS_URL = "http://xxx.xxxx.com.cn:8090/api/sendsms.php";
public static final String APP_TYPE = "jssjApp";
public static final String MSG_TYPE_EMAIL = "2";// 邮件
public static final String MSG_TYPE_SMS = "1";// 短信
public static final String SOURCE = "netgate";// 返回信息来源
public static final String STATUS_SUCCESS = "1";// 返回成功标志
private static final String APPLICATION_JSON = "application/json";
private static final String CONTENT_TYPE_TEXT_JSON = "text/json";
private static final String CHARSET_UTF_8 = "UTF-8";
/**
* 发送json数据到服务器网关接口
*
* @param url
* @param param
* @return
*/
public static String postJson(String url, JSONObject param) {
try {
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
// 绑定到请求 Entry
StringEntity se = new StringEntity(param.toString(), CHARSET_UTF_8);
se.setContentType(CONTENT_TYPE_TEXT_JSON);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
httpPost.setEntity(se);
// 发送请求
HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
// 得到应答的字符串,这也是一个 JSON 格式保存的数据
String resp = EntityUtils.toString(httpResponse.getEntity(), CHARSET_UTF_8);
System.out.println(resp);
return resp;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 组合电子邮箱发送数据
*
* @param email
* @param username
* @return
*/
private static String getFormatEmail(String email, String username) {
email = StringUtils.trim(email).toLowerCase();
username = StringUtils.trim(username);
if (StringUtils.isNotBlank(email)) {
if (StringUtils.isBlank(username)) {
username = "null";
}
return email + "/" + username;
}
return null;
}
/**
* 组合手机号发送数据
*
* @param phone
* @param username
* @return
*/
private static String getFormatPhone(String phone, String username) {
phone = StringUtils.trim(phone).toLowerCase();
username = StringUtils.trim(username);
if (StringUtils.isNotBlank(phone)) {
if (StringUtils.isBlank(username)) {
username = "null";
}
return phone + "/" + username;
}
return null;
}
/**
* 组合发送的json数据
*
* @param type
* @param toAddress
* @param username
* @param title
* @param content
* @return
*/
private static JSONObject getPostJson(String type, String toAddress, String username, String title, String content) {
JSONObject param = new JSONObject();
param.put("apptype", APP_TYPE);
param.put("msgtype", type);
param.put("title", title);
param.put("content", content);
if (type.equalsIgnoreCase(MSG_TYPE_EMAIL)) {// 邮件
param.put("sendto", getFormatEmail(toAddress, username));
} else if (type.equalsIgnoreCase(MSG_TYPE_SMS)) {// 短信
param.put("sendto", getFormatPhone(toAddress, username));
}
return param;
}
/**
* 发送单封邮件
*
* @param email
* @param username
* @param title
* @param content
* @return
*/
public static boolean sendEmail(String email, String username, String title, String content) {
// 处理参数信息,去空,邮件字母转小写
title = StringUtils.trim(title);
content = StringUtils.trim(content);
if (StringUtils.isBlank(username)) {
username = "null";
}
// 邮箱地址、内容均不为空时,发送邮件,否则不发送
if (StringUtils.isNotBlank(email) && StringUtils.isNotBlank(content)) {
// 先封装一个 JSON 对象
JSONObject param = new JSONObject();
param = getPostJson(MSG_TYPE_EMAIL, email, username, title, content);
String resp = postJson(NETGATE_EMAIL_URL, param);
// 解析返回的json数据
JSONObject respJson = JSONObject.fromObject(resp);
Integer status = (Integer) respJson.get("status");
if (status == 1) {
return true;
}
}
return false;
};
/**
* 发送单条短信
*
* @param phone
* @param username
* @param title
* @param content
* @return
*/
public static boolean sendSMS(String phone, String username, String title, String content) {
// 处理参数信息,去空,邮件字母转小写
title = StringUtils.trim(title);
content = StringUtils.trim(content);
if (StringUtils.isBlank(username)) {
username = "null";
}
// 邮箱地址、内容均不为空时,发送邮件,否则不发送
if (StringUtils.isNotBlank(phone) && StringUtils.isNotBlank(content)) {
// 先封装一个 JSON 对象
JSONObject param = new JSONObject();
param = getPostJson(MSG_TYPE_SMS, phone, username, title, content);
String resp = postJson(NETGATE_SMS_URL, param);
// 解析返回的json数据
JSONObject respJson = JSONObject.fromObject(resp);
String status = (String) respJson.get("status");
if ("1".equalsIgnoreCase(status)) {
return true;
}
}
return false;
};
public static void main(String[] args) {
// sendEmail("zhang_xxx@163.com", "zhang", "邮件标题测试", "邮件内容测试<a href='http://www.xxxxx.cn/'>二维码链接</a>");
sendSMS("18812345678", "张某某", "短信标题测试", "短信内容测试<a href='http://www.xxxxx.cn/'>二维码链接</a>");
}
}
httpClient 使用post方法提交json数据,接收返回数据实例
最新推荐文章于 2024-07-18 02:59:17 发布