HttpClient中post請求http、https示例


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

使用HttpClient發送請求、接收響應很簡單,一般需要如下幾步即可:

  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的訪問,采取繞過證書的策略:

    /**
     * 繞過驗證
     *     
     * @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或者其他工具生成的證書,還是希望用其他方式認證自簽名的證書,這篇文章就來分享一下如何設置信任自簽名的證書。當然你也可以參考官網示例中。

要想信任自簽名的證書,必須得知道密鑰庫的路徑及密鑰庫的密碼。然后加載到程序來才可以。具體代碼如下:

    /**
     * 設置信任自簽名證書
     *     
     * @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方法:

    /**
     * 模擬請求
     * 
     * @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;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM