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;
    }
   


猜你喜欢

转载自blog.csdn.net/demo_gsl/article/details/80881498