package com.ju.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import net.sf.json.JSONObject; import java.security.cert.X509Certificate; import javax.net.ssl.X509TrustManager; public class HttpPost { private final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; private static void trustAllHosts() { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[]{}; } public void checkClientTrusted(X509Certificate[] chain, String authType) { } public void checkServerTrusted(X509Certificate[] chain, String authType) { } }}; // Install the all-trusting trust manager try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); } } public static String https(String url, Map<String, String> params) throws Exception { // 構建請求參數 String result = ""; PrintWriter out = null; BufferedReader in = null; String sendString = ""; JSONObject json = JSONObject.fromObject(params); System.out.println("發送報文:" + json.toString()); sendString = json.toString(); System.out.println("ERP鏈接:" + url); System.out.println("發送給ERP信息:" + sendString); try { trustAllHosts(); URL url2 = new URL(url); HttpsURLConnection urlCon = (HttpsURLConnection) url2.openConnection(); urlCon.setHostnameVerifier(DO_NOT_VERIFY); urlCon.setDoOutput(true); urlCon.setDoInput(true); urlCon.setRequestMethod("POST"); urlCon.setRequestProperty("Content-type", "application/json;charset=UTF-8"); // 發送POST請求必須設置以下兩行 urlCon.setDoOutput(true); urlCon.setDoInput(true); // 獲取URLConnection對象對應的輸出流 OutputStream os = urlCon.getOutputStream(); //參數是鍵值隊 , 不以"?"開始 os.write(sendString.getBytes()); //os.write("googleTokenKey=&username=admin&password=5df5c29ae86331e1b5b526ad90d767e4".getBytes()); os.flush(); // 發送請求參數 //out.print(a); // flush輸出流的緩衝 //out.flush(); // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader( new InputStreamReader(urlCon.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception 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 String http(String url, Map<String, String> reqMap) throws Exception { URL u = null; HttpURLConnection con = null; // 構建請求參數 //StringBuffer sb = new StringBuffer(); String sendString = "" ; //String tradeCode = params.get(ParamsConfig.keyTradeCode); JSONObject json = JSONObject.fromObject(reqMap); System.out.println("發送報文:"+json.toString()); sendString = json.toString(); /*if(tradeCode.equals(TradeCode.TRANS_WXAPP_PAY) || tradeCode.equals(TradeCode.TRANS_APP_QUERY) || tradeCode.equals(TradeCode.TRANS_TYPE_COLLECT) || tradeCode.equals(TradeCode.TRANS_TYPE_QUERY_COLLECT_TXN)){ JSONObject json = JSONObject.fromObject(params); System.out.println("發送報文:"+json.toString()); sendString = json.toString(); } else { if (params != null) { for (Entry<String, String> e : params.entrySet()) { sb.append(e.getKey()); sb.append("="); sb.append(e.getValue()); sb.append("&"); } sendString = sb.substring(0, sb.length() - 1); } }*/ System.out.println("ERP鏈接:" + url); System.out.println("發送給ERP信息:" + sendString); // logger.info("ERP鏈接:" + url); // logger.info("發送給ERP信息:" + sb.toString()); // 嘗試發送請求 try { u = new URL(url); con = (HttpURLConnection) u.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); // con.setConnectTimeout(300*1000); // // con.setReadTimeout(300*1000); con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); OutputStreamWriter osw = new OutputStreamWriter(con .getOutputStream(), "UTF-8"); osw.write(sendString); osw.flush(); osw.close(); } catch (Exception e) { e.printStackTrace(); throw new Exception("與服務器鏈接發生錯誤:"+e.getMessage()); } finally { if (con != null) { con.disconnect(); } } // 讀取返回內容 StringBuffer buffer = new StringBuffer(); try { BufferedReader br = new BufferedReader(new InputStreamReader(con .getInputStream(), "UTF-8")); String temp; while ((temp = br.readLine()) != null) { buffer.append(temp); } } catch (Exception e) { e.printStackTrace(); throw new Exception("從服務器獲取數據失敗:"+e.getMessage()); } return buffer.toString(); } }