HttpClient 是 Apache Jakarta Common 下的子項目,能夠用來提供高效的、最新的、功能豐富的支持 HTTP 協議的客戶端編程工具包,而且它支持 HTTP 協議最新的版本和建議。java
使用HttpClient發送請求、接收響應很簡單,通常須要以下幾步便可:apache
具體代碼以下(HttpClient-4.4.1):編程
/** * 簡單httpclient實例 * * @author arron * @date 2015年11月11日 下午6:36:49 * @version 1.0 */ public class SimpleHttpClientDemo { /** * 模擬請求 * * @param url 資源地址 * @param map 參數列表 * @param encoding 編碼 * @return * @throws ParseException * @throws IOException */ public static String send(String url, Map<String,String> map,String encoding) throws ParseException, IOException{ String body = ""; //建立httpclient對象 CloseableHttpClient client = HttpClients.createDefault(); //建立post方式請求對象 HttpPost httpPost = new HttpPost(url); //裝填參數 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); if(map!=null){ for (Entry<String, String> entry : map.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } //設置參數到請求對象中 httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding)); System.out.println("請求地址:"+url); System.out.println("請求參數:"+nvps.toString()); //設置header信息 //指定報文頭【Content-type】、【User-Agent】 httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); //執行請求操做,並拿到結果(同步阻塞) CloseableHttpResponse response = client.execute(httpPost); //獲取結果實體 HttpEntity entity = response.getEntity(); if (entity != null) { //按指定編碼轉換結果實體爲String類型 body = EntityUtils.toString(entity, encoding); } EntityUtils.consume(entity); //釋放連接 response.close(); return body; } }
對於HTTPS的訪問,採起繞過證書的策略:tomcat
/** * 繞過驗證 * * @return * @throws NoSuchAlgorithmException * @throws KeyManagementException */ public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException { SSLContext sc = SSLContext.getInstance("SSLv3"); // 實現一個X509TrustManager接口,用於繞過驗證,不用修改裏面的方法 X509TrustManager trustManager = new X509TrustManager() { @Override public void checkClientTrusted( java.security.cert.X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException { } @Override public void checkServerTrusted( java.security.cert.X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException { } @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } }; sc.init(null, new TrustManager[] { trustManager }, null); return sc; }
而後修改原來的send方法:服務器
/** * 模擬請求 * * @param url 資源地址 * @param map 參數列表 * @param encoding 編碼 * @return * @throws NoSuchAlgorithmException * @throws KeyManagementException * @throws IOException * @throws ClientProtocolException */ public static String send(String url, Map<String,String> map,String encoding) throws KeyManagementException, NoSuchAlgorithmException, ClientProtocolException, IOException { String body = ""; //採用繞過驗證的方式處理https請求 SSLContext sslcontext = createIgnoreVerifySSL(); // 設置協議http和https對應的處理socket連接工廠的對象 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext)) .build(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); HttpClients.custom().setConnectionManager(connManager); //建立自定義的httpclient對象 CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build(); // CloseableHttpClient client = HttpClients.createDefault(); //建立post方式請求對象 HttpPost httpPost = new HttpPost(url); //裝填參數 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); if(map!=null){ for (Entry<String, String> entry : map.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } //設置參數到請求對象中 httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding)); System.out.println("請求地址:"+url); System.out.println("請求參數:"+nvps.toString()); //設置header信息 //指定報文頭【Content-type】、【User-Agent】 httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); //執行請求操做,並拿到結果(同步阻塞) CloseableHttpResponse response = client.execute(httpPost); //獲取結果實體 HttpEntity entity = response.getEntity(); if (entity != null) { //按指定編碼轉換結果實體爲String類型 body = EntityUtils.toString(entity, encoding); } EntityUtils.consume(entity); //釋放連接 response.close(); return body; }
可是,若是是本身用jdk或者其餘工具生成的證書,仍是但願用其餘方式認證自簽名的證書,這篇文章就來分享一下如何設置信任自簽名的證書。固然你也能夠參考官網示例中。app
要想信任自簽名的證書,必須得知道密鑰庫的路徑及密鑰庫的密碼。而後加載到程序來才能夠。具體代碼以下:socket
/** * 設置信任自簽名證書 * * @param keyStorePath 密鑰庫路徑 * @param keyStorepass 密鑰庫密碼 * @return */ public static SSLContext custom(String keyStorePath, String keyStorepass){ SSLContext sc = null; FileInputStream instream = null; KeyStore trustStore = null; try { trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); instream = new FileInputStream(new File(keyStorePath)); trustStore.load(instream, keyStorepass.toCharArray()); // 相信本身的CA和全部自簽名的證書 sc = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build(); } catch (KeyStoreException | NoSuchAlgorithmException| CertificateException | IOException | KeyManagementException e) { e.printStackTrace(); } finally { try { instream.close(); } catch (IOException e) { } } return sc; }
而後修改原來的send方法:ide
/** * 模擬請求 * * @param url 資源地址 * @param map 參數列表 * @param encoding 編碼 * @return * @throws ParseException * @throws IOException * @throws KeyManagementException * @throws NoSuchAlgorithmException * @throws ClientProtocolException */ public static String send(String url, Map<String,String> map,String encoding) throws ClientProtocolException, IOException { String body = ""; //tomcat是我本身的密鑰庫的密碼,你能夠替換成本身的 //若是密碼爲空,則用"nopassword"代替 SSLContext sslcontext = custom("D:\\keys\\wsriakey", "tomcat"); // 設置協議http和https對應的處理socket連接工廠的對象 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", new SSLConnectionSocketFactory(sslcontext)) .build(); PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); HttpClients.custom().setConnectionManager(connManager); //建立自定義的httpclient對象 CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build(); // CloseableHttpClient client = HttpClients.createDefault(); //建立post方式請求對象 HttpPost httpPost = new HttpPost(url); //裝填參數 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); if(map!=null){ for (Entry<String, String> entry : map.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } //設置參數到請求對象中 httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding)); System.out.println("請求地址:"+url); System.out.println("請求參數:"+nvps.toString()); //設置header信息 //指定報文頭【Content-type】、【User-Agent】 httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); //執行請求操做,並拿到結果(同步阻塞) CloseableHttpResponse response = client.execute(httpPost); //獲取結果實體 HttpEntity entity = response.getEntity(); if (entity != null) { //按指定編碼轉換結果實體爲String類型 body = EntityUtils.toString(entity, encoding); } EntityUtils.consume(entity); //釋放連接 response.close(); return body; }