okhttp3 https请求不需要证书

自测可用

1.证书类class

2.okhttp类class


   
   
   
   
  1. public class SSLSocketClient {
  2.      //获取这个SSLSocketFactory
  3.      public static SSLSocketFactory getSSLSocketFactory() {
  4.          try {
  5.             SSLContext sslContext = SSLContext.getInstance( "SSL");
  6.             sslContext.init( null, getTrustManager(), new SecureRandom());
  7.              return sslContext.getSocketFactory();
  8.         } catch (Exception e) {
  9.              throw new RuntimeException(e);
  10.         }
  11.     }
  12.      //获取TrustManager
  13.      private static TrustManager[] getTrustManager() {
  14.         TrustManager[] trustAllCerts = new TrustManager[]{
  15.              new X509TrustManager() {
  16.                  @Override
  17.                  public void checkClientTrusted(X509Certificate[] chain, String authType) {
  18.                 }
  19.                  @Override
  20.                  public void checkServerTrusted(X509Certificate[] chain, String authType) {
  21.                 }
  22.                  @Override
  23.                  public X509Certificate[] getAcceptedIssuers() {
  24.                      return new X509Certificate[]{}; //okhttp 3.0 以前版本
  25.                       return new X509Certificate[ 0] ; //3.0后版本
  26.                 }
  27.             }
  28.         };
  29.          return trustAllCerts;
  30.     }
  31.      //获取HostnameVerifier
  32.      public static HostnameVerifier getHostnameVerifier() {
  33.         HostnameVerifier hostnameVerifier = new HostnameVerifier() {
  34.              @Override
  35.              public boolean verify(String s, SSLSession sslSession) {
  36.                  return true;
  37.             }
  38.         };
  39.          return hostnameVerifier;
  40.     }
  41. }

 

 


   
   
   
   
  1. public class HttpUtil {
  2. private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
  3. private static final OkHttpClient client = new OkHttpClient().newBuilder()
  4. .connectTimeout( 200, TimeUnit.SECONDS)
  5. .readTimeout( 200, TimeUnit.SECONDS)
  6. .retryOnConnectionFailure( false)
  7. .sslSocketFactory(SSLSocketClient.getSSLScocketFactory())
  8. .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
  9. .build();
  10. private static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse( "application/json; charset=utf-8");
  11. private static final HttpUtil instance = new HttpUtil();
  12. private HttpUtil() {
  13. }
  14. public static HttpUtil getInstance() {
  15. return instance;
  16. }
  17. /**
  18. * 执行http post请求
  19. * @param postBody
  20. * @param url
  21. * @return
  22. */
  23. public String httpExecute(String postBody, String url,String tokenCode) {
  24. Request request = null ;
  25. if(tokenCode != null && ! "".equals(tokenCode)) {
  26. Headers headers = new Headers.Builder()
  27. .add( "Authorization",tokenCode)
  28. .add( "Content-type", "application/json;charset=utf-8")
  29. .build();
  30. request = new Request.Builder().url(url)
  31. .headers(headers)
  32. .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
  33. .build();
  34. } else {
  35. request = new Request.Builder()
  36. .url(url)
  37. .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
  38. .build();
  39. }
  40. try {
  41. System.out.println( "request:"+ request);
  42. Response response = client.newCall(request).execute();
  43. logger.info( "response:{}",response);
  44. return response.body().string();
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. logger.debug( " 网络调用异常httpExecute : {}", e);
  48. }
  49. return null;
  50. }
  51. /**
  52. * 转换成指定对象
  53. *
  54. * @param postBody
  55. * @param url
  56. * @param clazz
  57. * @return
  58. * @throws IOException
  59. */
  60. public Object httpExecute(String postBody, String url, Class clazz,String tokenCode) {
  61. String rString = httpExecute(postBody, url,tokenCode);
  62. Object obj = null ;
  63. try {
  64. Object obj = JSONObject.parseObject(rString, clazz);
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. return obj ;
  69. }
  70. }

 

你可能感兴趣的:(okhttp3 https请求不需要证书)