java接口请求工具类

Maven添加第三方类库

在这里我传的参数是Json格式的。

下面是java代码:

   /**
     * 招标接口输入参数,post请求
     *
     * @param json
     * @return
     */
    public static JSONObject doInvitePost(String url, TimersInviteInfo timersInviteInfo) {
        JSONObject param1 = new JSONObject();
        param1.put("METHOD", "GET_TISKLIST");
        
        JSONObject param2 = new JSONObject();
        param2.put("projectName", timersInviteInfo.getInviteProjectName());
        param2.put("tenderer", timersInviteInfo.getInviteUnit());
        param2.put("tendProjectCode", timersInviteInfo.getInviteProjectNum());
        param2.put("tendRegion", timersInviteInfo.getRegionName());
        param2.put("belongRegion", timersInviteInfo.getCityName());
        param2.put("publishDate", timersInviteInfo.getInviteTime());
        param2.put("closeDate", timersInviteInfo.getDeadlineTime());
        param2.put("startDate", timersInviteInfo.getBidOpenTime());
        param2.put("projectMoney", timersInviteInfo.getProjectMoney());
        param2.put("projectDesc", timersInviteInfo.getProjectIntroduction());
        param2.put("tendWeb", timersInviteInfo.getUrlName());
        param2.put("tendIp", timersInviteInfo.getInviteProjectUrl());
        JSONObject params = new JSONObject();
        params.put("HEADER", param1);
        JSONObject params2 = new JSONObject();
        params2.put("REQUEST_INFO", param2);
        params.put("BODY", params2);
        JSONObject param = new JSONObject();
        param.put("ROOT", params);
        System.out.println("param:" + param);
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        JSONObject response = null;
        try {
            StringEntity s = new StringEntity(param.toString());
            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(entity);// 返回json格式:
                response = JSONObject.fromObject(result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }

    /**
     * 中标接口输入参数,post请求
     *
     * @param json
     * @return
     */
    public static JSONObject doBidPost(String url, TimersBidInfo timersBidInfo) {
        JSONObject param1 = new JSONObject();
        param1.put("METHOD", "GET_TISKLIST");
        JSONObject param2 = new JSONObject();
        param2.put("projectName", timersBidInfo.getBidProjectName());
        param2.put("tenderer", timersBidInfo.getBiddingUnit());
        param2.put("winProjectCode", timersBidInfo.getBidProjectNum());
        param2.put("tendRegion", timersBidInfo.getRegionName());
        param2.put("belongRegion", timersBidInfo.getCityName());
        param2.put("winPublishDate", timersBidInfo.getBidTime());
        param2.put("winUnit", timersBidInfo.getBidUnit());
        param2.put("winProjectMoney", timersBidInfo.getBidProjectMoney());
        param2.put("winProjectDesc", timersBidInfo.getProjectIntroduction());
        System.out.println("=========================="+timersBidInfo.getProjectIntroduction());
        param2.put("winWeb", timersBidInfo.getUrlName());
        param2.put("winIp", timersBidInfo.getBidProjectUrl());
        JSONObject params = new JSONObject();
        params.put("HEADER", param1);
        JSONObject params2 = new JSONObject();
        params2.put("REQUEST_INFO", param2);
        params.put("BODY", params2);
        JSONObject param = new JSONObject();
        param.put("ROOT", params);
        System.out.println("param:" + param);
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        JSONObject response = null;
        try {
            StringEntity s = new StringEntity(param.toString());
            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(entity);// 返回json格式:
                response = JSONObject.fromObject(result);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }
   


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值