HTML 을 문자열 로 변환

4761 단어 자바
     ,     .


import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

public class test3 {
    @RequestMapping(value = "/static_resources",produces = "text/html; charset=utf-8")
    @ResponseBody
    public String static_resources( String path) throws KeyManagementException, NoSuchAlgorithmException, IOException {

        System.out.println ( "==============static_resources: " + path );

        String body = "";
        if(null != path) {
            //           https  
            SSLContext sslcontext=createIgnoreVerifySSL ();

            //    http https     socket       
            Registry socketFactoryRegistry=RegistryBuilder. 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 ();
            CloseableHttpResponse response=null;
            try {
                //  get      
                HttpGet get=new HttpGet ( path );

                //     Content-type、User-Agent
                get.setHeader ( "Content-type" , "application/x-www-form-urlencoded" );
                get.setHeader ( "User-Agent" , "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2" );

                //      ,     (    )
                response=client.execute ( get );

                //      
                HttpEntity entity=response.getEntity ();
                if (entity != null) {
                    //            String  
                    body=EntityUtils.toString ( entity , "UTF-8" );
                }
                EntityUtils.consume ( entity );
                System.out.println ( "body:" + body );
            } catch (ClientProtocolException e) {
                e.printStackTrace ();
            } catch (IOException e) {
                e.printStackTrace ();
            } finally {
                //    
                if (null != response) {
                    response.close ();
                }
                if (null != client) {
                    client.close ();
                }
            }
        }
        return body;
    }


    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;
    }

}

좋은 웹페이지 즐겨찾기