Java调用Http/Https接口(8)--OkHttp调用Http/Https接口

 OkHttp是一个高效的HTTP客户端,在Android中用的比较多,也可以用在Java中;本文主要介绍OkHttp在java中的使用,文中所使用到的软件版本:Java 1.8.0_191、SpringBoot 2.2.1.RELEASE。

1、OkHttp特点

a、支持HTTP/2,允许所有同一个主机地址的请求共享同一个socket连接
b、连接池减少请求延时
c、透明的GZIP压缩减少响应数据的大小
d、缓存响应内容,避免一些完全重复的请求

2、服务端

参见Java调用Http接口(1)--编写服务端

3、调用Http接口

添加依赖如下:

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.8.1</version>
</dependency>

由于okHttp 4.x用到了kotline,所以需要kotline的环境;在IDEA中可以直接下载kotline的插件。

3.1、GET请求

@Test
public void get() throws IOException {
    String requestPath = "http://localhost:8080/demo/httptest/getUser?userId=1000&userName=李白";
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(requestPath).get().build();
    Response response = client.newCall(request).execute();
    System.out.println("get返回状态:" + response.code());
    System.out.println("get返回结果:" + response.body().string());
    response.close();
}

3.2、POST请求(发送键值对数据)

@Test
public void post() throws IOException {
    String requestPath = "http://localhost:8080/demo/httptest/getUser";
    OkHttpClient client = new OkHttpClient();

    RequestBody body = new FormBody.Builder()
            .add("userId", "1000")
            .add("userName", "李白")
            .build();
    Request request = new Request.Builder()
            .url(requestPath).post(body).build();
    Response response = client.newCall(request).execute();
    System.out.println("post返回状态:" + response.code());
    System.out.println("post返回结果:" + response.body().string());
    response.close();
}

3.3、POST请求(发送JSON数据)

@Test
public void post2() throws IOException {
    String requestPath = "http://localhost:8080/demo/httptest/addUser";
    OkHttpClient client = new OkHttpClient();
    String param = "{\"userId\": \"1001\",\"userName\":\"杜甫\"}";
    RequestBody body = RequestBody.create(param, MediaType.get("application/json; charset=utf-8"));
    Request request = new Request.Builder()
            .url(requestPath)
            .post(body)
            .build();
    Response response = client.newCall(request).execute();
    System.out.println("post2返回状态:" + response.code());
    System.out.println("post2返回结果:" + response.body().string());
    response.close();
}

3.4、上传文件

@Test
public void upload() throws IOException {
    String requestPath = "http://localhost:8080/demo/httptest/upload";
    OkHttpClient client = new OkHttpClient();

    File file = new File("d:/a.jpg");
    RequestBody body = RequestBody.create(file, MediaType.get("file/*"));
    Request request = new Request.Builder()
            .url(requestPath)
            .post(body)
            .build();
    Response response = client.newCall(request).execute();
    System.out.println("upload返回状态:" + response.code());
    System.out.println("upload返回结果:" + response.body().string());
    response.close();
}

3.5、上传文件及发送键值对数据

@Test
public void mulit() throws IOException {
    String requestPath = "http://localhost:8080/demo/httptest/multi";
    OkHttpClient client = new OkHttpClient();
    File file = new File("d:/a.jpg");
    RequestBody body = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", file.getName(), RequestBody.create(file, MediaType.parse("image/png")))
            .addFormDataPart("param1", "参数1")
            .addFormDataPart("param2", "参数2")
            .build();
    Request request = new Request.Builder()
            .url(requestPath)
            .post(body)
            .build();
    Response response = client.newCall(request).execute();
    System.out.println("mulit返回状态:" + response.code());
    System.out.println("mulit返回结果:" + response.body().string());
    response.close();
}

3.6、完整例子

package com.abc.demo.http.client;

import okhttp3.*;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * 通过OkHttp调用Http接口
 */
public class OkHttpCase {
    /**
     * GET请求
     */
    @Test
    public void get() throws IOException {
        String requestPath = "http://localhost:8080/demo/httptest/getUser?userId=1000&userName=李白";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(requestPath).get().build();
        Response response = client.newCall(request).execute();
        System.out.println("get返回状态:" + response.code());
        System.out.println("get返回结果:" + response.body().string());
        response.close();
    }

    /**
     * POST请求(发送键值对数据)
     */
    @Test
    public void post() throws IOException {
        String requestPath = "http://localhost:8080/demo/httptest/getUser";
        OkHttpClient client = new OkHttpClient();

        RequestBody body = new FormBody.Builder()
                .add("userId", "1000")
                .add("userName", "李白")
                .build();
        Request request = new Request.Builder()
                .url(requestPath).post(body).build();
        Response response = client.newCall(request).execute();
        System.out.println("post返回状态:" + response.code());
        System.out.println("post返回结果:" + response.body().string());
        response.close();
    }

    /**
     * POST请求(发送json数据)
     */
    @Test
    public void post2() throws IOException {
        String requestPath = "http://localhost:8080/demo/httptest/addUser";
        OkHttpClient client = new OkHttpClient();
        String param = "{\"userId\": \"1001\",\"userName\":\"杜甫\"}";
        RequestBody body = RequestBody.create(param, MediaType.get("application/json; charset=utf-8"));
        Request request = new Request.Builder()
                .url(requestPath)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        System.out.println("post2返回状态:" + response.code());
        System.out.println("post2返回结果:" + response.body().string());
        response.close();
    }

    /**
     * 上传文件
     */
    @Test
    public void upload() throws IOException {
        String requestPath = "http://localhost:8080/demo/httptest/upload";
        OkHttpClient client = new OkHttpClient();

        File file = new File("d:/a.jpg");
        RequestBody body = RequestBody.create(file, MediaType.get("file/*"));
        Request request = new Request.Builder()
                .url(requestPath)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        System.out.println("upload返回状态:" + response.code());
        System.out.println("upload返回结果:" + response.body().string());
        response.close();
    }


    /**
     * 上传文件及发送键值对数据
     */
    @Test
    public void mulit() throws IOException {
        String requestPath = "http://localhost:8080/demo/httptest/multi";
        OkHttpClient client = new OkHttpClient();
        File file = new File("d:/a.jpg");
        RequestBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getName(), RequestBody.create(file, MediaType.parse("image/png")))
                .addFormDataPart("param1", "参数1")
                .addFormDataPart("param2", "参数2")
                .build();
        Request request = new Request.Builder()
                .url(requestPath)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        System.out.println("mulit返回状态:" + response.code());
        System.out.println("mulit返回结果:" + response.body().string());
        response.close();
    }
}
OkHttpCase.java

4、调用Https接口

与调用Http接口不一样的部分主要在设置sslSocketFactory和hostnameVerifier部分,下面用GET请求来演示sslSocketFactory和hostnameVerifier的设置,其他调用方式类似。

package com.abc.demo.http.client;

import com.abc.demo.common.util.FileUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.Test;

import javax.net.ssl.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * 通过OkHttp调用Https接口
 */
public class OkHttpHttpsCase {
    /*
     * 请求有权威证书的地址
     */
    @Test
    public void test() throws IOException {
        String requestPath = "https://www.baidu.com/";
        OkHttpClient client = new OkHttpClient.Builder()
                //.connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS))
                .build();
        Request request = new Request.Builder()
                .url(requestPath).get().build();
        Response response = client.newCall(request).execute();
        System.out.println("get返回状态:" + response.code());
        System.out.println("get返回结果:" + response.body().string());
        response.close();
    }

    /**
     * 请求自定义证书的地址,不需要客户端证书
     *
     * @throws Exception
     */
    @Test
    public void test2() throws Exception {
        String requestPath = "https://10.49.196.10:9010/myservice";
        //获取信任证书库
        KeyStore trustStore = getkeyStore("jks", "d:/temp/cacerts", "123456");
        //KeyStore trustStore = null; //trustStore为null也可以
        OkHttpClient client = new OkHttpClient.Builder()
                .sslSocketFactory(getSSLSocketFactory(null, null, trustStore), new DefaultTrustManager())
                .hostnameVerifier((s, sslSession) -> true).build();
        Request request = new Request.Builder()
                .url(requestPath).get().build();
        Response response = client.newCall(request).execute();
        System.out.println("get返回状态:" + response.code());
        System.out.println("get返回结果:" + response.body().string());
        response.close();
    }

    /**
     * 请求自定义证书的地址,需要客户端证书
     *
     * @throws IOException
     */
    @Test
    public void test3() throws Exception {
        String requestPath = "https://10.49.196.10:9016/myservice";
        //获取客户端证书,,客户端证书可以是用 keytool 生成的 pks12 格式密钥库,也可以是用 OpenSSL 生成的 pkcs12 格式证书。
        KeyStore keyStore = getkeyStore("pkcs12", "d:/client.p12", "123456");

        //获取信任证书库
        KeyStore trustStore = getkeyStore("jks", "d:/temp/cacerts", "123456");
        //KeyStore trustStore = null; //trustStore为null也可以
        OkHttpClient client = new OkHttpClient.Builder()
                .sslSocketFactory(getSSLSocketFactory(keyStore, "123456", trustStore), new DefaultTrustManager())
                .hostnameVerifier((s, sslSession) -> true).build();
        Request request = new Request.Builder()
                .url(requestPath).get().build();
        Response response = client.newCall(request).execute();
        System.out.println("get返回状态:" + response.code());
        System.out.println("get返回结果:" + response.body().string());
        response.close();
    }

    /**
     * 获取证书
     *
     * @return
     */
    private KeyStore getkeyStore(String type, String filePath, String password) {
        KeyStore keySotre = null;
        FileInputStream in = null;
        try {
            keySotre = KeyStore.getInstance(type);
            in = new FileInputStream(new File(filePath));
            keySotre.load(in, password.toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            FileUtil.close(in);
        }
        return keySotre;
    }

    private SSLSocketFactory getSSLSocketFactory(KeyStore keyStore, String keyStorePassword, KeyStore trustStore) throws Exception {
        KeyManager[] keyManagers = null;
        TrustManager[] trustManagers = null;
        if (keyStore != null) {
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
            keyManagerFactory.init(keyStore, keyStorePassword.toCharArray());
            keyManagers = keyManagerFactory.getKeyManagers();
        }
        if (trustStore != null) {
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
            trustManagerFactory.init(trustStore);
            trustManagers = trustManagerFactory.getTrustManagers();
        } else {
            trustManagers = new TrustManager[]{new DefaultTrustManager()};
        }

        //设置服务端支持的协议
        SSLContext context = SSLContext.getInstance("TLSv1.2");
        context.init(keyManagers, trustManagers, null);
        SSLSocketFactory sslFactory = context.getSocketFactory();

        return sslFactory;
    }

    private final class DefaultTrustManager implements 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 new X509Certificate[]{};
        }
    }
}

 

posted @ 2019-12-01 10:15  且行且码  阅读(3294)  评论(0编辑  收藏  举报