okhttp3 忽略SSL,图片下载BASE64

okhttp3 忽略SSL,图片下载BASE64,第1张

用okhttp3获取图片数据,忽略SSL验证,获取到数据后base64

先配置jar(这是pom.xml的配置,其他的直接下载加到项目里就好)

        
		
            com.squareup.okhttp3
            okhttp
            3.6.0
        

        
      

上源码



import java.io.IOException;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Arrays;

import javax.net.ssl.*;

import okhttp3.*;
import sun.misc.BASE64Encoder;

public class Test {

	public static void main(String[] args) {
		OkHttpClient client = new OkHttpClient().newBuilder()
				.sslSocketFactory(getSSLSocketFactory(), getX509TrustManager())
				.hostnameVerifier(getHostnameVerifier())
				.build();
		Request request = new Request.Builder().url("http://www.kaotop.com/file/tupian/20220514/64c150837af2e58eaa04d78bb1a7c737.jpg")
				.method("GET", null).build();
		try {
			Response response = client.newCall(request).execute();
			byte[] bytes = response.body().bytes();
			BASE64Encoder encoder = new BASE64Encoder();
			String data = encoder.encode(bytes);// 返回Base64编码过的字节数组字符串
			System.out.println(data);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 获取这个SSLSocketFactory
	public static SSLSocketFactory getSSLSocketFactory() {
		try {
			SSLContext sslContext = SSLContext.getInstance("SSL");
			sslContext.init(null, getTrustManager(), new SecureRandom());
			return sslContext.getSocketFactory();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	// 获取TrustManager
	private static TrustManager[] getTrustManager() {
		return new TrustManager[] { new X509TrustManager() {
			@Override
			public void checkClientTrusted(X509Certificate[] chain, String authType) {
			}

			@Override
			public void checkServerTrusted(X509Certificate[] chain, String authType) {
			}

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

	// 获取HostnameVerifier
	public static HostnameVerifier getHostnameVerifier() {
		return (s, sslSession) -> true;
	}

	public static X509TrustManager getX509TrustManager() {
		X509TrustManager trustManager = null;
		try {
			TrustManagerFactory trustManagerFactory = TrustManagerFactory
					.getInstance(TrustManagerFactory.getDefaultAlgorithm());
			trustManagerFactory.init((KeyStore) null);
			TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
			if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
				throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
			}
			trustManager = (X509TrustManager) trustManagers[0];
		} catch (Exception e) {
			e.printStackTrace();
		}

		return trustManager;
	}
}

示例中的图片通过 URL url = new URL() 的方式是获取不到图片数据的,过滤SSL也无效。现在通过这个方法就能获取到图片数据了。

调用某些需要证书验证的也可以通过这个方式来调用。

图片下载到本地可以参考 [java] 图片与base64之间的互相转换_weixin_41608476的博客-CSDN博客_java 图片base64

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/919558.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存