HttpClient中post請求http、https示例

HttpClient 是 Apache Jakarta Common 下的子項目,能夠用來提供高效的、最新的、功能豐富的支持 HTTP 協議的客戶端編程工具包,而且它支持 HTTP 協議最新的版本和建議。java

使用HttpClient發送請求、接收響應很簡單,通常須要以下幾步便可:apache

  1. 建立CloseableHttpClient對象。
  2. 建立請求方法的實例,並指定請求URL。若是須要發送GET請求,建立HttpGet對象;若是須要發送POST請求,建立HttpPost對象。
  3. 若是須要發送請求參數,可可調用setEntity(HttpEntity entity)方法來設置請求參數。setParams方法已過期(4.4.1版本)。
  4. 調用HttpGet、HttpPost對象的setHeader(String name, String value)方法設置header信息,或者調用setHeaders(Header[] headers)設置一組header信息。
  5. 調用CloseableHttpClient對象的execute(HttpUriRequest request)發送請求,該方法返回一個CloseableHttpResponse。
  6. 調用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務器的響應內容。程序可經過該對象獲取服務器的響應內容;調用CloseableHttpResponse的getAllHeaders()、getHeaders(String name)等方法可獲取服務器的響應頭。
  7. 釋放鏈接。不管執行方法是否成功,都必須釋放鏈接

具體代碼以下(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;
    }
相關文章
相關標籤/搜索