通过http协议get,post推送消息,json格式

本文介绍了一种处理HTTP请求并进行消息推送的方法,包括GET和POST请求的处理,以及针对不同类型的通知(如会议通知、逾期通知等)的推送实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.smart.sso.server.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.smart.sso.server.model.HttpMap;
import com.smart.sso.server.model.Key;
import com.smart.sso.server.model.Keyword;
import com.smart.sso.server.model.SuperviseMeeting;

import net.sf.json.JSONObject;

/**
 * Created by liqun.chen on 2017/5/15.
 */
public class HttpUtil {
    /**
     * json 字符串
     * 
     * @param url
     * @param param
     * @return
     */
    public static String getSerchPersion(String url, String param) {
        /* 1 生成 HttpClinet 对象并设置参数 */
        HttpClient httpClient = new HttpClient();
        // 设置 Http 连接超时为5秒
        httpClient.getHttpConnectionManager().getParams()
                .setConnectionTimeout(5000);
        /* 2 生成 GetMethod 对象并设置参数 */
        GetMethod getMethod = new GetMethod(url);
        // 设置 get 请求超时为 5 秒
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
        // 设置请求重试处理,用的是默认的重试处理:请求三次
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler());
        String response = "";
        /* 3 执行 HTTP GET 请求 */
        try {
            int statusCode = httpClient.executeMethod(getMethod);
            /* 4 判断访问的状态码 */
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("请求出错: " + getMethod.getStatusLine());
            }
            /* 5 处理 HTTP 响应内容 */
            // HTTP响应头部信息,这里简单打印
            Header[] headers = getMethod.getResponseHeaders();
            for (Header h : headers)
                System.out
                        .println(h.getName() + "------------ " + h.getValue());
            // 读取 HTTP 响应内容,这里简单打印网页内容
            byte[] responseBody = getMethod.getResponseBody();// 读取为字节数组
            response = new String(responseBody, param);
            System.out.println("----------response:" + response);
            // 读取为 InputStream,在网页内容数据量大时候推荐使用
            // InputStream response = getMethod.getResponseBodyAsStream();
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("请检查输入的URL!");
            e.printStackTrace();
        } catch (IOException e) {
            // 发生网络异常
            System.out.println("发生网络异常!");
            e.printStackTrace();
        } finally {
            /* 6 .释放连接 */
            getMethod.releaseConnection();
        }
        return response;
    }

    /**
     * post请求
     * 
     * @param url
     * @param json
     * @return
     */
    public static JSONObject doPost(String url, JSONObject json) {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        JSONObject response = null;
        try {
            System.out.println("消息推送参数:" + json.toString());
            StringEntity s = new StringEntity(json.toString(), "UTF-8");
//            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");// 发送json数据需要设置contentType
            post.setEntity(s);
            HttpResponse res = client.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = res.getEntity();
                String result = EntityUtils.toString(res.getEntity());// 返回json格式:
                response = JSONObject.fromObject(result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }

    /**
     *  会议通知
     * @param m
     * @param type
     * @return
     * @throws Exception
     */
    public static String  pusHytz(SuperviseMeeting m,String type,List<String> list) throws Exception {
        String url = "推送的地址";
        JSONObject params = new JSONObject();
        Key Key = new Key();
        Key.setKey("first");
        Key.setValue("会议通知内容如下");
        Key.setColor("#333");
        Key Key1 = new Key();
        Key1.setKey("keyword1");
        Key1.setValue(m.getStartTime());
        Key1.setColor("#333");
        Key Key2 = new Key();
        Key2.setKey("keyword2");
        Key2.setValue(m.getTitle());
        Key2.setColor("#333");
        Key Key3 = new Key();
        Key3.setKey("keyword3");
        Key3.setValue(m.getAddress());
        Key3.setColor("#333");
        Key Key4 = new Key();
        Key4.setKey("keyword4");
        Key4.setValue(m.getObjectName());
        Key4.setColor("#333");
        Key Key5 = new Key();
        Key5.setKey("remark");
        Key5.setValue(m.getExplain());
        Key5.setColor("#333");
        HttpMap<Key> map = new HttpMap<Key>();
        List<Key> keyList = new ArrayList<Key>();
        List<String> user =new ArrayList<String>();
        keyList.add(Key);
        keyList.add(Key1);
        keyList.add(Key2);
        keyList.add(Key3);
        keyList.add(Key4);
        map.setData(keyList);
        //需要发送任务的用户id
        for (int i = 0; i < list.size(); i++) {
            user.add(list.get(i));
        }
        map.setUserid(user);
        map.setMsgtype(type);
        map.setData(keyList);
        params.put("data", map);

        // post 请求
        JSONObject jsonObject = doPost(url, params.getJSONObject("data"));
        System.out.println(jsonObject.toString());
        return jsonObject.toString();
    }
       /**
        *  逾期通知
        * @param k
        * @return
        * @throws Exception
        */
        public static String  pushYqtz(Keyword k,List<String> list) throws Exception {
            String url = "推送的地址";
            JSONObject params = new JSONObject();
            Key Key = new Key();
            Key.setKey("first");
            Key.setValue("任务逾期通知");
            Key.setColor("#333");
            Key Key1 = new Key();
            Key1.setKey("keyword1");
            //企业名称
            Key1.setValue(k.getKeyword1());
            Key1.setColor("#333");
            Key Key2 = new Key();
            Key2.setKey("keyword2");
            //提醒内容
            Key2.setValue(k.getKeyword2());
            Key2.setColor("#333");
            Key Key3 = new Key();
            Key3.setKey("keyword3");
            //时间
            Key3.setValue(k.getKeyword3());
            Key3.setColor("#333");
            Key Key4 = new Key();
            Key4.setKey("remark");
            //remark
            Key4.setValue(k.getRemark());
            Key4.setColor("#333");
            HttpMap<Key> map = new HttpMap<Key>();
            List<Key> keyList = new ArrayList<Key>();
            List<String> user =new ArrayList<String>();
            keyList.add(Key);
            keyList.add(Key1);
            keyList.add(Key2);
            keyList.add(Key3);
            keyList.add(Key4);
            map.setData(keyList);
            if(list.size()>0) {
                for (int i = 0; i < list.size(); i++) {
                    user.add(list.get(i));
                }
            }
            map.setUserid(user);
            //user.add("11390");
//            map.setUserid(user);
            map.setMsgtype("yqtz");
            map.setData(keyList);
            params.put("data", map);
            // post 请求
            JSONObject jsonObject = doPost(url, params.getJSONObject("data"));
            System.out.println(jsonObject.toString());
            return jsonObject.toString();
        }
        
          /**
           *  任务通知
           * @param k
           * @return
           * @throws Exception
           */
        public static String  pushRwdb(Keyword k,List<String> list) throws Exception {
            String url = "推送的地址";
            JSONObject params = new JSONObject();
            Key Key = new Key();
            Key.setKey("first");
            Key.setValue("新督办任务通知");
            Key.setColor("#333");
            Key Key1 = new Key();
            Key1.setKey("keyword1");
            //督办主题
            Key1.setValue(k.getKeyword1());
            Key1.setColor("#333");
            Key Key2 = new Key();
            Key2.setKey("keyword2");
            //开始时间
            Key2.setValue(k.getKeyword2());
            Key2.setColor("#333");
            Key Key3 = new Key();
            Key3.setKey("keyword3");
            //结束时间
            Key3.setValue(k.getKeyword3());
            Key3.setColor("#333");
            //督办人员
            Key Key5 = new Key();
            Key5.setKey("keyword4");
            Key5.setValue(k.getKeyword4());
            Key5.setColor("#333");
            //督办内容
            Key Key6 = new Key();
            Key6.setKey("keyword5");
            Key6.setValue(k.getKeyword5());
            Key6.setColor("#333");
            //remark
            Key Key4 = new Key();
            Key4.setKey("remark");
            Key4.setValue(k.getRemark());
            Key4.setColor("#333");
            HttpMap<Key> map = new HttpMap<Key>();
            List<Key> keyList = new ArrayList<Key>();
            List<String> user =new ArrayList<String>();
            keyList.add(Key);
            keyList.add(Key1);
            keyList.add(Key2);
            keyList.add(Key3);
            keyList.add(Key4);
            keyList.add(Key5);
            keyList.add(Key6);
            map.setData(keyList);
            //需要发送任务的用户id
            for (int i = 0; i < list.size(); i++) {
                user.add(list.get(i));
            }
            map.setUserid(user);
            map.setMsgtype("rwdb");
            map.setData(keyList);
            params.put("data", map);

            // post 请求
            JSONObject jsonObject = doPost(url, params.getJSONObject("data"));
            System.out.println(jsonObject.toString());
        return jsonObject.toString();
    }
        
        /**
         * 手动触发催办任务
         * @param k
         * @param list
         * @return
         * @throws Exception
         */
        public static String  pushSDCB(Keyword k,List<String> list) throws Exception {
            String url = "推送的地址";
            JSONObject params = new JSONObject();
            Key Key = new Key();
            Key.setKey("first");
            Key.setValue("催办通知 ");
            Key.setColor("#333");
            //申报用户
            Key Key1 = new Key();
            Key1.setKey("keyword1");
            Key1.setValue(k.getKeyword1());
            Key1.setColor("#333");
            //项目情况
            Key Key2 = new Key();
            Key2.setKey("keyword2");
            Key2.setValue(k.getKeyword2());
            Key2.setColor("#333");
            //催办时间
            Key Key3 = new Key();
            Key3.setKey("keyword3");
            Key3.setValue(k.getKeyword3());
            Key3.setColor("#333");
            //remark
            Key Key4 = new Key();
            Key4.setKey("remark");
            Key4.setValue(k.getRemark());
            Key4.setColor("#333");
            HttpMap<Key> map = new HttpMap<Key>();
            List<Key> keyList = new ArrayList<Key>();
            List<String> user =new ArrayList<String>();
            keyList.add(Key);
            keyList.add(Key1);
            keyList.add(Key2);
            keyList.add(Key3);
            keyList.add(Key4);
            map.setData(keyList);
            //需要发送任务的用户id
            for (int i = 0; i < list.size(); i++) {
                user.add(list.get(i));
            }
            map.setUserid(user);
            map.setMsgtype("ddrwcb");
            map.setData(keyList);
            params.put("data", map);
            
            // post 请求
            JSONObject jsonObject = doPost(url, params.getJSONObject("data"));
            System.out.println(jsonObject.toString());
            return jsonObject.toString();
        }
}
这是我在下项目中写的一个推送消息的方法,可以根据自己的具体情况修改

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值