HttpClient工具类

2年前 (2022) 程序员胖胖胖虎阿
261 0 0

POST请求

/**
     *
     * @param url       请求路径
     * @param header    请求头
     * @param param     对象参数
     * @param path      拼接参数
     * @param type      拼接路径true:PathVariable拼接,false:RequestParam拼接,""为不拼接
     * @return
     */
    public static String isPost(String url,Map<String,String> header,Object param,
                                Map<String,String> path,String type){
//        创建
        CloseableHttpClient httpClient=HttpClientBuilder.create().build();
        CloseableHttpResponse response=null;
        String uri="";
        try {
//            是否需要路径拼接
            if (!type.isEmpty()){
                if (type.equals("true")){
//                PathVariable拼接
                    if (!path.isEmpty()){
                        for (String key:path.keySet()){
                            uri+="/"+path.get(key);
                        }
//                        System.out.println("拼接路径: "+uri);
                    }
                }else if (type.equals("false")){
//                RequestParam拼接
                    if (!path.isEmpty()){
                        uri="?";
                        for (String key:path.keySet()){
                            uri+=key+"="+path.get(key)+"&";
                        }
                        uri=uri.substring(0,uri.length()-1);
                    }
                }
            }
            url=url+uri;
            System.out.println("访问路径: "+url);
//            HttpPost
            HttpPost httpPost = new HttpPost(url);
//            判断是否为空
            if (!header.isEmpty()){
                for (String key:header.keySet()){
//                    存入header
                    httpPost.addHeader(key,header.get(key));
                }
            }
            httpPost.setHeader("Content-Type", "application/json;charset=utf8");
//            转化为JSON串
            String jsonString = JSON.toJSONString(param);
            StringEntity entity = new StringEntity(jsonString, "UTF-8");
//            传入参数
            httpPost.setEntity(entity);
//            获取响应体
            response=httpClient.execute(httpPost);
//            获取响应体状态
//            System.out.println(response.getStatusLine().getStatusCode());
//            HttpClient只允许获取一次getEntity
            String result = EntityUtils.toString(response.getEntity());
            System.out.println("响应状态: "+response.getStatusLine().getStatusCode());
            System.out.println("响应数据: "+result);
            return result;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "执行发生错误!";
    }

GET

    /**
     * httpClient get请求
     * @param url 请求url
     * @param param 请求参数 form提交适用
     * @return 可能为空 需要处理
     * @throws Exception
     *
     */
    public static String sendHttpsGet(String  url, String param, String sign) throws Exception {
        String result = "";
        CloseableHttpClient httpClient = null;
        try {
            httpClient = getHttpClient();

            JSONObject parse = (JSONObject) JSONObject.parse(param);
            Integer pageCount = (Integer) parse.get("pageCount");
            Integer pageNo = (Integer) parse.get("pageNo");
            if (pageCount==pageNo){

            }else{
                url+="?pageCount="+pageCount+"/pageNo="+pageNo;
            }
            log.info("请求地址: "+url);
            HttpGet httpGet = new HttpGet(url);
            /**
             * 设置请求头
             */
            httpGet.addHeader("Content-Type", "application/json");
            httpGet.addHeader("Authorization",sign);
//            httpPost.addHeader("passWord","3er4#ER$3er4#ER$12");
            /**
             *  设置请求参数
             */
//            System.out.println("参数   "+param);
//            StringEntity stringEntity = new StringEntity(param, "utf-8");
//            httpGet.setEntity(stringEntity);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            int statusCode = httpResponse.getStatusLine().getStatusCode();

            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity resEntity = httpResponse.getEntity();
                result = EntityUtils.toString(resEntity);
            } else {
                result = readHttpResponse(httpResponse);
            }

        } catch (Exception e) {
            log.info("send lookalike http get request failed, HttpException"+e);
            throw e;
        } finally {
          
        }
      
        return result;
    }
版权声明:程序员胖胖胖虎阿 发表于 2022年11月21日 下午11:56。
转载请注明:HttpClient工具类 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...