java获取网页

来源:互联网 发布:哈工程教务处网络选课 编辑:程序博客网 时间:2024/05/18 00:35

已收录到http://www.qq-live.com/index.php?app=blog&id=1

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package http_request;


import java.io.BufferedReader;
import java.io.IOException;


import java.net.MalformedURLException;


import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;




import java.util.logging.Level;
import java.util.logging.Logger;


import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
/**
 * @author think
 */
public class WebPageReader
{
    public static String readContentsFromUrl(String urlString)
    {
    /*
     *  fix for
     *    Exception in thread "main" javax.net.ssl.SSLHandshakeException:
     *       sun.security.validator.ValidatorException:
     *           PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:
     *               unable to find valid certification path to requested target
     */
    TrustManager[] trustAllCerts = new TrustManager[] {
       new X509TrustManager() {
          public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
          }


          public void checkClientTrusted(X509Certificate[] certs, String authType) {  }


          public void checkServerTrusted(X509Certificate[] certs, String authType) {  }


       }
    };


    SSLContext sc = null;
    try {
        sc = SSLContext.getInstance("SSL");
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(WebPageReader.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
    } catch (KeyManagementException ex) {
        Logger.getLogger(WebPageReader.class.getName()).log(Level.SEVERE, null, ex);
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());


    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
          return true;
        }
    };
    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    /*
     * end of the fix
     */
        
        URLConnection connection = null;
        BufferedReader in = null;
        String content = null;
        try
        {
            URL url = new URL(urlString);
            connection = url.openConnection();


            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));


            String newLine = System.getProperty("line.separator");
            String inputLine = null;
            StringBuilder stringBuilder = new StringBuilder();
            while ((inputLine = in.readLine()) != null)
            {
                stringBuilder.append(inputLine).append(newLine);
            }
            content = stringBuilder.toString();
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(in != null)
            {
                try
                {
                    in.close();
                }
                catch (IOException e)
                {
                    //Ignore
                }
            }
        }
        System.out.println(content);
        return content;
    }
}
原创粉丝点击