Java8中基于OkHttp3编写HTTP2客户端详解

显然,我们必须在Java还没有准备好支持HTTP2客户端编程的情况下(即使用Java 8及以下版本),提供HTTP2客户端应用的解决方案。目前流行的类库如下:

 

  • OkHttp
  • Eclipse Jetty
  • Netty
  • Apache HttpComponents (Apache HC)

其中,Eclipse Jetty和Netty都同时提供客户端和服务端。而曾经广泛使用的Apache HC已经逐渐掉队,至今没有正式支持HTTP2的版本发布。所以这里特别推荐OkHttp,而且OkHttp还适合在Android应用中作为HTTP客户端。

OkHttp的最新版本3.10.0,2018年2月25日发布,特性如下:

 

  • 支持HTTP2,默认采用TLS 1.2
  • GZIP压缩
  • 缓存响应对象

在Maven项目中,pom.xml配置如下:


  com.squareup.okhttp3
  okhttp
  3.10.0

编写HTTP2客户端应用如下:

1. 创建OkHttpClient

1.1 默认配置

OkHttpClient client = new OkHttpClient();

 

1.2 定制配置

 

OkHttpClient client = new OkHttpClient
	.Builder()
	.authenticator(new Authenticator() {...}
	.build();

2. 创建请求

2.1 GET请求

Request request = new Request.Builder()
        .url("https://www.mydomain.com/")
        .build();//GET by default

2.2 POST请求

RequestBody strBody = RequestBody.create(MediaType.parse("text/x-markdown; charset=utf-8"), postBodyStr);
RequestBody jsonBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), postBodyJsonStr);
Request request = new Request.Builder()
        .url("https://www.mydomain.com")
        .post(mybody)
        .build();

3. 发送请求并处理响应

3.1 同步请求

Response response = client.newCall(request).execute(){//synch call
	if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
		System.out.println(response.body().string());
}

3.2 异步请求

client.newCall(request).enqueue(new Callback() {//asynch call
      @Override public void onFailure(Call call, IOException e) {
        e.printStackTrace();
      }

      @Override public void onResponse(Call call, Response response) throws IOException {
        try (ResponseBody responseBody = response.body()) {
          if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

          Headers responseHeaders = response.headers();
          for (int i = 0, size = responseHeaders.size(); i < size; i++) {
            System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
          }

          System.out.println(responseBody.string());
        }
      }
});

4. OkHttp访问HTTPS服务

final TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager() {
        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {}

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {}

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[]{};
        }
    }
};

final HostnameVerifier verifiedAllHostname = new HostnameVerifier() {
    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
};

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

OkHttpClient client = new OkHttpClient.Builder()
    .sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
    .hostnameVerifier(verifiedAllHostname)
    .connectTimeout(60, TimeUnit.SECONDS)
    .readTimeout(60, TimeUnit.SECONDS)
    .writeTimeout(60, TimeUnit.SECONDS)
    .retryOnConnectionFailure(true)
    .build();

Request request = new Request.Builder()
    .url("https://localhost:9443/hello")
    .build();

Response response = client.newCall(request).execute();
System.out.println(response.body().string());

这里我们只使用OkHttp原生API编写HTTP2客户端应用。在Spring Boot 2.0版本中,我们还可以使用Spring Boot starters for OkHttp,辅助编写HTTP2客户端。

 

参考链接:

http://square.github.io/okhttp/

https://www.eclipse.org/jetty

http://netty.io/

https://hc.apache.org/

https://github.com/freefair/okhttp-spring-boot

你可能感兴趣的:(HTTP2)