1、get请求工具类 public static String requestGet(String url) throws Exception { String strResult = null; try{ HttpClient httpsClient = HttpsClient.getInstance(); HttpGet request = new HttpGet(url); HttpResponse response = httpsClient.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { strResult = EntityUtils.toString(response.getEntity()); url = URLDecoder.decode(url, "UTF-8"); } else { log.error("httpclientGet通信异常:{}" + url); } }catch (IOException e) { log.error("httpclientGet通信IO异常:{}:{}", url, ExceptionMessageHandler.toString(e)); throw new Exception(e); } catch (NoSuchAlgorithmException e) { log.error("httpclientGet异常:{}:{}", url, ExceptionMessageHandler.toString(e)); throw new Exception(e); } catch (KeyManagementException e) { throw new Exception(e); } catch (Exception e){ log.error("httpclientGet异常:{}:{}", url, ExceptionMessageHandler.toString(e)); throw new Exception(e); } return strResult; } 2、post请求方式,Content-type为application/json public static String requestPost(String url,Map<String, Object> params){ CloseableHttpClient httpClient = HttpClients.createDefault(); // 1 创建一个post对象 HttpPost post = new HttpPost(url); post.addHeader("Content-type","application/json; charset=utf-8"); post.setHeader("Accept", "application/json"); // 2 创建一个Entity。模拟一个xml System.out.println("params-->>"+params ); StringEntity stringEntity = new StringEntity(params.toString(),"UTF-8"); stringEntity.setContentEncoding("UTF-8"); post.setEntity(stringEntity); // 3 执行post请求 String responseResult =""; try { CloseableHttpResponse response = httpClient.execute(post); //if(response.getStatusLine().getStatusCode() == 200) { HttpEntity he = response.getEntity(); String respContent = EntityUtils.toString(he,"UTF-8"); responseResult = respContent; //} System.out.println(responseResult); response.close(); httpClient.close(); }catch (Exception e) { e.printStackTrace(); } return responseResult; } 3、post请求方式,Content-type为application/x-www-form-urlencoded public static String requestXPost(String url,String params,String accessToken){ CloseableHttpClient httpClient = HttpClients.createDefault(); // 1 创建一个post对象 HttpPost post = new HttpPost(url); //post.addHeader("Content-type","application/json; charset=utf-8");/ //设置请求头,应用监控的请求头权限 post.addHeader("Authoriz",accessToken); post.setHeader("Accept", "application/json, text/plain, */*"); post.addHeader("Content-Type", "application/x-www-form-urlencoded"); // 添加请求头 // 2 创建一个Entity。模拟一个xml System.out.println("params-->>"+params ); //StringEntity stringEntity = new StringEntity(params,"UTF-8"); //stringEntity.setContentEncoding("UTF-8"); //post.setEntity(stringEntity); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("businessId", params)); UrlEncodedFormEntity urlEncodedFormEntity = null; try { urlEncodedFormEntity = new UrlEncodedFormEntity(nvps, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } post.setEntity(urlEncodedFormEntity); // 3 执行post请求 String responseResult =""; try { CloseableHttpResponse response = httpClient.execute(post); HttpEntity he = response.getEntity(); String respContent = EntityUtils.toString(he,"UTF-8"); responseResult = respContent; System.out.println(responseResult); response.close(); httpClient.close(); }catch (Exception e) { e.printStackTrace(); } return responseResult; } }
HttpsClient 的三种请求方式的工具类
最新推荐文章于 2024-07-10 09:39:47 发布