
本文共 4310 字,大约阅读时间需要 14 分钟。
������������������8.0HTTPS������������������
���������������8.0���������������������toImage������������https���������������SSL���������������������������������������������������������������������������������������������������������������������������������������������HTTPS������������������������������������������������������������SSL������������������������������������������������������������������������
1. ���������������������������������������������������������������������
static {
SSLContext sslContext = SSLContext.getInstance("TLS");
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
};
sslContext.init(null, new TrustManager[] { trustManager }, null);
}
2. ������HttpClient������������������������
private static HttpClient getHttpClient() {
return new DefaultHttpClient(connectionManager, httpParams);
private static HttpClient httpsTrustClient() {
HttpClient base = getHttpClient();
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return new DefaultHttpClient(base.getConnectionManager(), base.getParams());
} catch (Exception e) {
e.printStackTrace();
return null;
}
3. ������requestImg������������������������������URL������
private BufferedImage requestImg(String imageUrl) {
try {
if (imageUrl == null || !imageUrl.startsWith("http://") && !imageUrl.startsWith("https://")) {
// ������������http/https���������������������������
imageUrl = "http:" + imageUrl;
}
HttpClient clientId = httpsTrustClient();
HttpPost httpRequest = new HttpPost(imageUrl);
List
params = new ArrayList (); params.add(new NameValuePair("imageRequest", "1"));
try {
bootstrap������mam���� HttpEntity entity = clientId.execute(httpRequest);
if (entity != null) {
HttpContent content = entity.getContent();
return ImageIO.read(content);
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
4. ���Html������������������������������������������������������������
���������������������������
������������������������������������������������������������������ ������HttpClient���������������������������HTTPS������ ���requestImg������������������������������������ ������URL������������������������������������null��������� ������������������������������������������������8.0���������HTTPS���������������SSL������������������������������������������������������������������������������������������������������������������������������������������������toImage���������������������������
������������������������������������������������������������������������������������������������������������������
- ���������������������������������������static���������������������������
- ������������������������network���IO������������������try-catch������������������������������������������
- ���������������������������������������������������������������������
���������������������������������������������������������������������URI���������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
