package com.happy.common.http; import com.alibaba.fastjson.JSONObject; import com.happy.common.wx.WxConfig; import com.happy.common.wx.WxConstants; import com.happy.common.wx.WxUtil; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * HttpsClient类 * @author lujunjie * @date 2018/03/01 */ public class HttpsClient { /** * GET请求方式 */ public static final String METHOD_GET = "GET"; /** * POST请求方式 */ public static final String METHOD_POST = "POST"; /** * 连接超时时间 */ private static Integer CONNECTION_TIMEOUT = WxConfig.connectionTimeout; /** * 请求超时时间 */ private static Integer READ_TIMEOUT = WxConfig.readTimeout; /** * 发起https请求 * @param requestUrl 请求地址 * @param requestMethod 请求方式(Get或者post) * @param postData 提交数据 * @return JSONObject */ public static JSONObject httpsRequestReturnJSONObject(String requestUrl, String requestMethod, String postData) throws Exception{ JSONObject jsonObject = JSONObject.parseObject(HttpsClient.httpsRequestReturnString(requestUrl,requestMethod,postData)); System.out.println("jsonObjectDate: " + jsonObject); return jsonObject; } /** * 发起https请求 * @param requestUrl 请求地址 * @param requestMethod 请求方式(Get或者post) * @param postData 提交数据 * @return String */ public static String httpsRequestReturnString(String requestUrl, String requestMethod, String postData) throws Exception{ String response; HttpsURLConnection httpsUrlConnection = null; try{ //创建https请求证书 TrustManager[] tm={new MyX509TrustManager()}; //创建SSLContext管理器对像,使用我们指定的信任管理器初始化 SSLContext sslContext=SSLContext.getInstance("SSL","SunJSSE"); sslContext.init(null, tm, new java.security.SecureRandom()); SSLSocketFactory ssf=sslContext.getSocketFactory(); // 创建URL对象 URL url= new URL(requestUrl); // 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象 httpsUrlConnection=(HttpsURLConnection)url.openConnection(); //设置ssl证书 httpsUrlConnection.setSSLSocketFactory(ssf); //设置header信息 httpsUrlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //设置User-Agent信息 httpsUrlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"); //设置可接受信息 httpsUrlConnection.setDoOutput(true); //设置可输入信息 httpsUrlConnection.setDoInput(true); //不使用缓存 httpsUrlConnection.setUseCaches(false); //设置请求方式(GET/POST) httpsUrlConnection.setRequestMethod(requestMethod); //设置连接超时时间 if (CONNECTION_TIMEOUT > 0) { httpsUrlConnection.setConnectTimeout(CONNECTION_TIMEOUT); } else { //默认10秒超时 httpsUrlConnection.setConnectTimeout(10000); } //设置请求超时 if (READ_TIMEOUT > 0) { httpsUrlConnection.setReadTimeout(READ_TIMEOUT); } else { //默认10秒超时 httpsUrlConnection.setReadTimeout(10000); } //设置编码 httpsUrlConnection.setRequestProperty("Charsert", WxConstants.DEFAULT_CHARSET); //判断是否需要提交数据 if(StringUtils.equals(requestMethod,HttpsClient.METHOD_POST) && StringUtils.isNotBlank(postData)){ //讲参数转换为字节提交 byte[] bytes = postData.getBytes(WxConstants.DEFAULT_CHARSET); //设置头信息 httpsUrlConnection.setRequestProperty("Content-Length", Integer.toString(bytes.length)); //开始连接 httpsUrlConnection.connect(); //防止中文乱码 OutputStream outputStream=httpsUrlConnection.getOutputStream(); outputStream.write(postData.getBytes(WxConstants.DEFAULT_CHARSET)); outputStream.flush(); outputStream.close(); }else{ //开始连接 httpsUrlConnection.connect(); } response = WxUtil.getStreamString(httpsUrlConnection.getInputStream()); }catch (Exception e){ throw new Exception(); }finally { if (httpsUrlConnection != null) { // 关闭连接 httpsUrlConnection.disconnect(); } } return response; } public static void get(String url, Map params){ CloseableHttpClient httpClient = null; HttpGet httpGet = null; try { httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build(); String ps = ""; for (String pKey : params.keySet()) { if(!"".equals(ps)){ ps = ps + "&"; } ps = pKey+"="+params.get(pKey); } if(!"".equals(ps)){ url = url + "?" + ps; } httpGet = new HttpGet(url); httpGet.setConfig(requestConfig); CloseableHttpResponse response = httpClient.execute(httpGet); HttpEntity httpEntity = response.getEntity(); System.out.println(EntityUtils.toString(httpEntity,"utf-8")); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(httpGet!=null){ httpGet.releaseConnection(); } if(httpClient!=null){ httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static String get(String strURL) throws Exception{ URL url = new URL(strURL); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); httpConn.setRequestMethod("GET"); httpConn.connect(); System.out.println("bbb: "+httpConn.getResponseCode()); BufferedReader reader = new BufferedReader(new InputStreamReader( httpConn.getInputStream(),"utf-8")); String line; StringBuffer buffer = new StringBuffer(); while ((line = reader.readLine()) != null) { buffer.append(line); } reader.close(); httpConn.disconnect(); return buffer.toString(); } /** * 发送 post请求 * @param * @param */ public static void post(String url, Map params){ CloseableHttpClient httpClient = null; HttpPost httpPost = null; try { httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build(); httpPost = new HttpPost(url); httpPost.setConfig(requestConfig); List ps = new ArrayList(); for (String pKey : params.keySet()) { ps.add(new BasicNameValuePair(pKey, params.get(pKey))); } httpPost.setEntity(new UrlEncodedFormEntity(ps)); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity httpEntity = response.getEntity(); System.out.println(EntityUtils.toString(httpEntity,"utf-8")); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(httpPost!=null){ httpPost.releaseConnection(); } if(httpClient!=null){ httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } } //url表示请求链接,param表示json格式的请求参数 //自定义菜单创建访问方式 public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 注意Authorization生成 // conn.setRequestProperty("Content-Type", // "application/x-www-form-urlencoded"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"utf-8")); // 发送请求参数 out.print("&"+param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream(),"utf-8")); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送 请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } //url表示请求链接,param表示json格式的请求参数 //自定义菜单创建访问方式 public static String sendPost2(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { HttpsURLConnection httpsUrlConnection = null; //创建https请求证书 TrustManager[] tm={new MyX509TrustManager()}; //创建SSLContext管理器对像,使用我们指定的信任管理器初始化 SSLContext sslContext=SSLContext.getInstance("SSL","SunJSSE"); sslContext.init(null, tm, new java.security.SecureRandom()); SSLSocketFactory ssf=sslContext.getSocketFactory(); // 创建URL对象 URL realUrl= new URL(url); // 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象, // 打开和URL之间的连接 httpsUrlConnection=(HttpsURLConnection)realUrl.openConnection(); //设置ssl证书 httpsUrlConnection.setSSLSocketFactory(ssf); // 设置通用的请求属性 注意Authorization生成 // conn.setRequestProperty("Content-Type", // "application/x-www-form-urlencoded"); // 发送POST请求必须设置如下两行 httpsUrlConnection.setRequestMethod("POST"); httpsUrlConnection.setDoOutput(true); httpsUrlConnection.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(new OutputStreamWriter(httpsUrlConnection.getOutputStream(),"utf-8")); // 发送请求参数 out.print("&"+param); // flush输出流的缓冲 out.flush(); if (httpsUrlConnection.getResponseCode()==200) { // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(httpsUrlConnection.getInputStream(), "utf-8")); String line; while ((line = in.readLine()) != null) { result += line; } }else { result = "获取输入流异常!"; } } catch (Exception e) { System.out.println("发送 请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } public static void main(String[] args) { Map req = new HashMap<>(); req.put("operator_id", "000000002"); req.put("operator_secret", "SBC88d0ItR3w6oCTY"); req.put("data_secret", "Epc3hhs0imtVhPSu"); req.put("data_secret_iv", "r57m1sr8Tg2E302L"); req.put("sign_key", "ikAhygeGCRnEdhjgBKf"); } }