package com.olm.del; import com.olm.util.JSONUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.util.HashMap; import java.util.Map; /** * ClassName:HttpClientUtil Package:com.olm.util Date:2020/7/27 Auth:penghui@olm.com.cn */ /** * HttpClient工具类 */ public class HttpClientUtilBak { /**请求编码*/ private static final String DEFAULT_CHARSET = "UTF-8"; /** * 执行HTTP POST请求 * @param url url * @param param 参数 * @return */ public static String httpPostWithJSON(String url, Map param) { CloseableHttpClient client = null; try { if (url == null || url.trim().length() == 0) { throw new Exception("URL is null"); } HttpPost httpPost = new HttpPost(url); client = HttpClients.createDefault(); if (param != null) { StringEntity entity = new StringEntity(JSONUtils.toJSONString(param), DEFAULT_CHARSET);//解决中文乱码问题 entity.setContentEncoding(DEFAULT_CHARSET); entity.setContentType("application/json"); httpPost.setEntity(entity); } HttpResponse resp = client.execute(httpPost); if (resp.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(resp.getEntity(), DEFAULT_CHARSET); } } catch (Exception e) { e.printStackTrace(); } finally { close(client); } return null; } /** * 执行HTTP GET请求 * @param url url * @param param 参数 * @return */ public static String httpGetWithJSON(String url, Map param) { CloseableHttpClient client = null; try { if (url == null || url.trim().length() == 0) { throw new Exception("URL is null"); } client = HttpClients.createDefault(); if (param != null) { StringBuffer sb = new StringBuffer("?"); for (String key : param.keySet()) { sb.append(key).append("=").append(param.get(key)).append("&"); } url = url.concat(sb.toString()); url = url.substring(0, url.length() - 1); } HttpGet httpGet = new HttpGet(url); httpGet.addHeader("Accept", "text/html"); httpGet.addHeader("Accept-Charset", "utf-8"); httpGet.addHeader("Accept-Encoding", "gzip"); httpGet.addHeader("Accept-Language", "en-US,en"); //httpGet.addHeader("Authorization","Bearer ca4b6e4550d791b636e9a1d06869985fc0c2e1314ba85392a76bc94fc58b3ca9.MzYxNzk1"); httpGet.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.160 Safari/537.22"); HttpResponse resp = client.execute(httpGet); if (resp.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(resp.getEntity(), DEFAULT_CHARSET); } } catch (Exception e) { e.printStackTrace(); } finally { close(client); } return null; } /** * 关闭HTTP请求 * @param client */ private static void close(CloseableHttpClient client) { if (client == null) { return; } try { client.close(); } catch (Exception e) { } } public static void main(String[] args) throws Exception { Map param = new HashMap(); param.put("grant_type", "password"); param.put("Client_Id", "c97fbd8aca50ba94068da6ded23905df"); param.put("Client_Secret", "7a4d178ebf1d55229fd28c5cb18703da"); param.put("Redirect_Uri", "https://api.xiaoshouyi.com"); param.put("userName", ""); param.put("password", ""); String result = httpPostWithJSON("https://api.xiaoshouyi.com/oauth2/token", param); //String result = httpGetWithJSON("https://api.xiaoshouyi.com/oauth2/token", param); System.out.println("result:" + result); } }