httpclient發起https請求以及擷取https返回內容,httpclienthttps

來源:互聯網
上載者:User

httpclient發起https請求以及擷取https返回內容,httpclienthttps

 

工作中的需要,使用Apache httpclient發起擷取通過https返回的內容,試了很多網上的解決辦法都不行,查閱了Apache httpclient的官方文檔,最後終於找出解決方案,分享給需要的朋友,免得走彎路。

使用時,調用getSSL方法即可擷取https地址返回的內容。

代碼如下:

 

public static String getSSL(String url, String responseDecode) throws IOException {CloseableHttpClient httpclient = createSSLInsecureClient();CloseableHttpResponse response = null;String content = null;try {HttpGet httpget = new HttpGet(url);RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000)                        .setConnectionRequestTimeout(5000).build();httpget.setConfig(requestConfig);response = httpclient.execute(httpget);HttpEntity entity = response.getEntity();if (entity != null) {content = EntityUtils.toString(entity);EntityUtils.consume(entity);}} catch (ParseException e) {logger.error("request URL[" + url + "] error", e);throw e;} catch (IOException e) {logger.error("request URL[" + url + "] error", e);throw e;} finally {if (response != null) {try {response.close();} catch (IOException e) {logger.error("", e);}}if (httpclient != null) {try {httpclient.close();} catch (IOException e) {logger.error("", e);}}}return content;}private static CloseableHttpClient createSSLInsecureClient() {try {SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {@Overridepublic boolean isTrusted(java.security.cert.X509Certificate[] arg0, String arg1) {return true;}}).build();SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);return HttpClients.custom().setSSLSocketFactory(sslsf).build();} catch (KeyManagementException e) {e.printStackTrace();} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (KeyStoreException e) {e.printStackTrace();}return HttpClients.createDefault();}public static void main(String[] args) {ShiroUser  user = UMSHelper.invoke("10001", "10102130023000");System.out.println(user);}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.