使用Java从https获取图像
有没有一种方法可以通过Java从https url获取图像 到目前为止,我正在尝试的是:使用Java从https获取图像,java,https,Java,Https,有没有一种方法可以通过Java从https url获取图像 到目前为止,我正在尝试的是: 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
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
我怎样才能度过难关?我必须在该url上获取超过6k个图像。在获取图像之前,您必须首先解决SSL问题。它说在JAVA_HOME/jre/lib/security中的受信任存储中找不到任何名为ns6.host.md的受信任主机。您可以将该主机的公钥添加到信任库中,或者忽略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];
}
}
有两个问题。您可以使用浏览器访问站点,并查看错误
HostnameVerifier
在我看来,您需要为您的主机提供有效的证书。现在您正在使用Parallels的默认证书,该证书对主机名无效。因此,您会得到一个
证书例外情况。那么,您能帮我举个例子吗?:)@Denees如果您感兴趣,我知道如何使用bayou HttpClient。请看@Denees-请随时问我任何问题。使用一个新的图书馆(我的)是有风险的;但好的一面是,你会得到作者的充分关注:)
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);
}