使用Java从https获取图像

时间:2015-05-13 20:28:23

标签: java https

有没有办法从带有Java的https网址获取图片?

到目前为止我在尝试:

URL url = new URL("https://ns6.host.md:8443/sitepreview/http/zugo.md/media/images/thumb/23812__yu400x250.jpg");

System.out.println("Image: " + ImageIO.read(url));

但是,我得到了:

Exception in thread "main" javax.imageio.IIOException: Can't get input stream from URL!
Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No 
Caused by: java.security.cert.CertificateException: No name matching ns6.host.md found

我该怎么办呢?我在那个网址上有超过6k的图像,我必须抓取它。

2 个答案:

答案 0 :(得分:1)

有两个问题。您可以使用浏览器访问该站点,并查看错误。

  1. 服务器证书是自签名的,不受Java信任。您可以将其添加到信任存储区。

  2. 服务器证书与主机名" ns6.host.md"不匹配,您需要忽略它的HostnameVerifier

  3. 另一个答案说同样的事情,它提供了代码,遗憾的是它使用了一些私有API。

    如果有人感兴趣,如何在bayou HttpClient中解决它的示例: https://gist.github.com/zhong-j-yu/22af353e2c5a5aed5857

    public static void main(String[] args) throws Exception
    {
        HttpClient client = new HttpClientConf()
            .sslContext(new SslConf().trustAll().createContext()) // trust self-signed certs
            .sslEngineConf(engine -> disableHostNameVerification(engine))
            .trafficDump(System.out::print)
            .newClient();
        // typically, app creates one client and use it for all requests
    
        String url = "https://ns6.host.md:8443/sitepreview/http/zugo.md/media/images/thumb/23812__yu400x250.jpg";
        HttpResponse response = client.doGet(url).sync();
        ByteBuffer bb = response.bodyBytes(Integer.MAX_VALUE).sync();
    
        InputStream is = new ByteArrayInputStream(bb.array(), bb.arrayOffset()+bb.position(), bb.remaining());
        BufferedImage image = ImageIO.read(is);
    
    }
    
    static void disableHostNameVerification(SSLEngine engine)
    {
        SSLParameters sslParameters = engine.getSSLParameters();
        {
            // by default, it's set to "HTTPS", and the server certificate must match the request host.
            // disable it for this example, since the server certificate is ill constructed.
            sslParameters.setEndpointIdentificationAlgorithm(null);
        }
        engine.setSSLParameters(sslParameters);
    }
    

答案 1 :(得分:0)

在获取图片之前,您必须首先解决SSL问题。它表示在JAVA_HOME / jre / lib / security中的受信任存储中找不到名为ns6.host.md的任何可信主机。您可以向您的TrustStore添加该主机的公钥,或者只是忽略SSL错误:

HttpsURLConnection.setDefaultHostnameVerifier(getUnsecureHostNameVerifier());

try {
        SSLContext e = SSLContext.getInstance("TLS");
        e.init(new KeyManager[0], new TrustManager[]{new DefaultTrustManager()}, new SecureRandom());
        SSLContext.setDefault(e);
        HttpsURLConnection.setDefaultSSLSocketFactory(e.getSocketFactory());
    } catch (Exception var1) {
        throw new Exception("SSL Error", var1);
    }

 public static class DefaultTrustManager implements X509TrustManager {
    public DefaultTrustManager() {
    }

    public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
    }

    public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
    }

    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[0];
    }
}