import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; 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;
1 /** 2 * 發起post請求 有參數的情況 3 * @param url 4 * @param params 5 * @param codePage 6 * @return 7 * @throws Exception 8 */ 9 private synchronized static String postData(String url, Map<String, Object> params, String codePage) throws Exception { 10 //創建post請求對象 11 HttpPost httppost = new HttpPost(url); 12 // 獲取到httpclient客戶端 13 CloseableHttpClient httpclient = HttpClients.createDefault(); 14 try { 15 //創建參數集合 16 List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); 17 //添加請求頭參數 18 if(url.equals(GetOlapDataUrl)) { 19 httppost.addHeader("Content-Type", "application/json"); 20 httppost.addHeader("accessToken",params.get("accessToken").toString()); 21 } 22 // 設置請求的一些配置設置,主要設置請求超時,連接超時等參數 23 RequestConfig requestConfig = RequestConfig.custom() 24 .setConnectTimeout(10000).setConnectionRequestTimeout(10000).setSocketTimeout(10000) 25 .build(); 26 httppost.setConfig(requestConfig); 27 //添加參數 28 httppost.setEntity(new StringEntity(JSONObject.toJSONString(params), ContentType.create("application/json", "utf-8"))); 29 // 請求結果 30 String resultString = ""; 31 //啟動執行請求,並獲得返回值 32 CloseableHttpResponse response = httpclient.execute(httppost); 33 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 34 // 獲取請求響應結果 35 HttpEntity entity = response.getEntity(); 36 if (entity != null) { 37 // 將響應內容轉換為指定編碼的字符串 38 resultString = EntityUtils.toString(entity, "UTF-8"); 39 System.out.printf("Response content:{}", resultString); 40 return resultString; 41 } 42 } else { 43 System.out.println("請求失敗!"); 44 return resultString; 45 } 46 } catch (Exception e) { 47 throw e; 48 } finally { 49 httpclient.close(); 50 } 51 return null; 52 } 53 54 /** 55 * 發起post請求 沒有任何參數的情況 56 * 57 * @param url 請求的目標url地址 58 * @param otherParam 其他參數 59 * @param data 請求數據 60 * @return 將響應結果轉換成string返回 61 * @throws IOException 可能出現的異常 62 */ 63 private static String postMsg(String url, String otherParam) throws IOException { 64 // 根據url地址發起post請求 65 HttpPost httppost = new HttpPost(url); 66 // 獲取到httpclient客戶端 67 CloseableHttpClient httpclient = HttpClients.createDefault(); 68 try { 69 // 設置請求的一些頭部信息 70 httppost.addHeader("Content-Type", "application/json"); 71 httppost.addHeader("accessToken", otherParam); 72 // 設置請求的一些配置設置,主要設置請求超時,連接超時等參數 73 RequestConfig requestConfig = RequestConfig.custom() 74 .setConnectTimeout(10000).setConnectionRequestTimeout(10000).setSocketTimeout(10000) 75 .build(); 76 httppost.setConfig(requestConfig); 77 // 執行請求 78 CloseableHttpResponse response = httpclient.execute(httppost); 79 // 請求結果 80 String resultString = ""; 81 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 82 // 獲取請求響應結果 83 HttpEntity entity = response.getEntity(); 84 if (entity != null) { 85 // 將響應內容轉換為指定編碼的字符串 86 resultString = EntityUtils.toString(entity, "UTF-8"); 87 System.out.printf("Response content:{}", resultString); 88 return resultString; 89 } 90 } else { 91 System.out.println("請求失敗!"); 92 return resultString; 93 } 94 } catch (Exception e) { 95 throw e; 96 } finally { 97 httpclient.close(); 98 } 99 return null; 100 }
/** * 發起post請求 有參數的情況 * @param url * @param params * @param codePage * @return * @throws Exception */ private synchronized static String postData(String url, Map<String, Object> params, String codePage) throws Exception { //創建post請求對象 HttpPost httppost = new HttpPost(url); // 獲取到httpclient客戶端 CloseableHttpClient httpclient = HttpClients.createDefault(); try { //創建參數集合 List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); //添加請求頭參數 if(url.equals(GetOlapDataUrl)) { httppost.addHeader("Content-Type", "application/json"); httppost.addHeader("accessToken",params.get("accessToken").toString()); } // 設置請求的一些配置設置,主要設置請求超時,連接超時等參數 RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(10000).setConnectionRequestTimeout(10000).setSocketTimeout(10000) .build(); httppost.setConfig(requestConfig); //添加參數 httppost.setEntity(new StringEntity(JSONObject.toJSONString(params), ContentType.create("application/json", "utf-8"))); // 請求結果 String resultString = ""; //啟動執行請求,並獲得返回值 CloseableHttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 獲取請求響應結果 HttpEntity entity = response.getEntity(); if (entity != null) { // 將響應內容轉換為指定編碼的字符串 resultString = EntityUtils.toString(entity, "UTF-8"); System.out.printf("Response content:{}", resultString); return resultString; } } else { System.out.println("請求失敗!"); return resultString; } } catch (Exception e) { throw e; } finally { httpclient.close(); } return null; }/** * 發起post請求 沒有任何參數的情況 * * @param url 請求的目標url地址 * @param otherParam 其他參數 * @param data 請求數據 * @return 將響應結果轉換成string返回 * @throws IOException 可能出現的異常 */ private static String postMsg(String url, String otherParam) throws IOException { // 根據url地址發起post請求 HttpPost httppost = new HttpPost(url); // 獲取到httpclient客戶端 CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 設置請求的一些頭部信息 httppost.addHeader("Content-Type", "application/json"); httppost.addHeader("accessToken", otherParam); // 設置請求的一些配置設置,主要設置請求超時,連接超時等參數 RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(10000).setConnectionRequestTimeout(10000).setSocketTimeout(10000) .build(); httppost.setConfig(requestConfig); // 執行請求 CloseableHttpResponse response = httpclient.execute(httppost); // 請求結果 String resultString = ""; if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 獲取請求響應結果 HttpEntity entity = response.getEntity(); if (entity != null) { // 將響應內容轉換為指定編碼的字符串 resultString = EntityUtils.toString(entity, "UTF-8"); System.out.printf("Response content:{}", resultString); return resultString; } } else { System.out.println("請求失敗!"); return resultString; } } catch (Exception e) { throw e; } finally { httpclient.close(); } return null; }