httpClient 使用post方法提交json数据,接收返回数据实例
来源:互联网 发布:淘宝女运动服套装冬款 编辑:程序博客网 时间:2024/05/16 18:40
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);// 绑定到请求 EntryStringEntity 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>");}}
0 0
- httpClient 使用post方法提交json数据,接收返回数据实例
- Java 通过HttpClient Post方式提交json,并从服务端返回json数据
- HttpClient Post提交数据
- ajax提交json数据并使用Springmvc接收实例
- post接收json数据
- HttpClient和HttpUrlConnection实现post发送和接收json数据
- post提交json数据后,java接收json
- httpclient使用post方式发送json数据,以及服务端的接收
- 【httpclient编写爬虫】post提交json数据和普通键值
- POST提交JSON数据
- 使用jQuery POST提交数据返回的JSON是字符串不能解析为JSON对象
- 使用jQuery POST提交数据返回的JSON是字符串不能解析为JSON对象
- 使用jQuery POST提交数据返回的JSON是字符串不能解析为JSON对象
- 使用Volley提交Json数据post
- 使用Retrofit通过post提交Json数据
- http post发送与接收返回json数据
- httpClient post发送json数据
- 使用httpclient提交数据
- 12. JPA EntityManager persist 方法详解
- [BZOJ2286][SDOI2011]消耗战
- 聚类1-K-means-EM算法
- /etc/securetty和禁止telnet root登录
- pat 1001 - 1004
- httpClient 使用post方法提交json数据,接收返回数据实例
- openGL之圆环---openGL学习笔记(七)
- java 调用webservice 接口 解析返回json
- centOS 开机自启动配置方法
- 线性代数:第四章 向量组的线性相关性(1)向量组的线性相关性 向量组的秩
- 低功耗蓝牙(BLE)官方文档翻译
- android 开发技巧(17)--ListView的选择模式
- android在线源码浏览网站
- 正则表达式学习--RegexBuddy工具的使用(一)