乐趣区

关于java:HttpClient工具类

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;
    }
退出移动版