java使用https获取页面_使用Java从https获取图像

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

>服务器证书是自签名的,不受Java信任.您可以将其添加到信任库.

>服务器证书与主机名“ns6.host.md”不匹配,您需要一个忽略它的HostnameVerifier.

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

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值