javax.net.ssl.HttpsURLConnection返回火星诗歌

时间:2013-05-17 14:23:56

标签: java http ssl https

我正在编写一个简单的https客户端,它将通过https下载网页的html。我可以很好地连接到网页,但我下载的HTML是乱码。

public String GetWebPageHTTPS(String URI){
    BufferedReader read;
    URL inputURI;
    String line;
    String renderedPage = "";
    try{
        inputURI = new URL(URI);
        HttpsURLConnection connect;
        connect = (HttpsURLConnection)inputURI.openConnection();
        connect.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401");
        read = new BufferedReader (new InputStreamReader(connect.getInputStream()));
        while ((line = read.readLine()) != null)
            renderedPage += line;
        read.close();
    }
    catch (MalformedURLException e){
        e.printStackTrace();
    }
    catch (IOException e){
        e.printStackTrace();
    }
    return renderedPage;
}

当我传递一个像https://kat.ph/这样的字符串时,会返回大约10,000个字符的乱码

修改的 这是我修改的自签名证书代码,但我仍然得到加密流:

public String GetWebPageHTTPS(String URI){
    TrustManager[] trustAllCerts = new TrustManager[] { 
            new X509TrustManager() {     
                public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
                    return null;
                } 
                public void checkClientTrusted( 
                    java.security.cert.X509Certificate[] certs, String authType) {
                    } 
                public void checkServerTrusted( 
                    java.security.cert.X509Certificate[] certs, String authType) {
                }
            } 
        }; 
        try {
            SSLContext sc = SSLContext.getInstance("SSL"); 
            sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (GeneralSecurityException e) {
        } 
        try { 
            System.out.println("URI: " + URI);
            URL url = new URL(URI); 
        } catch (MalformedURLException e) {
        } 
    BufferedReader read;
    URL inputURI;
    String line;
    String renderedPage = "";
    try{
        inputURI = new URL(URI);
        HttpsURLConnection connect;
        connect = (HttpsURLConnection)inputURI.openConnection();
        read = new BufferedReader (new InputStreamReader(connect.getInputStream()));
        while ((line = read.readLine()) != null)
            renderedPage += line;
        read.close();
    }
    catch (MalformedURLException e){
        e.printStackTrace();
    }
    catch (IOException e){
        e.printStackTrace();
    }
    return renderedPage;
}

2 个答案:

答案 0 :(得分:1)

  

“它是否被任何机会压缩?stackoverflow.com/questions/8249522 / ...” - Mahesh Guruswamy

是的,事实证明这只是gzip压缩在这里是我的工作

public String GetWebPageGzipHTTP(String URI){ 
    String html = "";
    try {
        URLConnection connect = new URL(URI).openConnection();                        
        BufferedReader in = null;
        connect.setReadTimeout(10000);
        connect.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401");
        if (connect.getHeaderField("Content-Encoding")!=null && connect.getHeaderField("Content-Encoding").equals("gzip")){
            in = new BufferedReader(new InputStreamReader(new GZIPInputStream(connect.getInputStream())));            
        } else {
            in = new BufferedReader(new InputStreamReader(connect.getInputStream()));            
        }          
        String inputLine;
        while ((inputLine = in.readLine()) != null){
        html+=inputLine;
        }
    in.close();
        return html;
    } catch (Exception e) {
        return html;
    }
}

}

答案 1 :(得分:0)

HTTPS始终显示证书,并且进一步的通信发生在安全的加密通道上。这就是为什么你收到的东西看起来像胡言乱语。

对于任何已签名的证书,HttpsURLConnection将为您完成工作,一切正常。如果证书未由证书颁发机构签名,则事情会变得混乱。在这种情况下,如果您从浏览器打开该URL,它将显示证书供您检查并接受,然后再继续。

在这里看起来你有类似的问题。您需要做的是告诉Java接受自签名证书而不抱怨。这里有两个选项,要么下载证书(只需在任何浏览器中打开URL,它将告诉你如何),并将其添加到JVM的密钥库中,或者创建自己的TrustManager并禁用证书验证。

有关这两个选项的详细信息,请参阅此SO答案。 https://stackoverflow.com/a/2893932/2385178