-
Java调用Http/Https接口(8,end)--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.inspur.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(); } }
4、调用Https接口
与调用Http接口不一样的部分主要在设置sslSocketFactory和hostnameVerifier部分,下面用GET请求来演示sslSocketFactory和hostnameVerifier的设置,其他调用方式类似。
package com.inspur.demo.http.client; import com.inspur.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.40.103.48:9010/zsywservice"; //获取信任证书库 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.40.103.48:9016/zsywservice"; //获取客户端证书 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[]{}; } } }
来源:https://www.cnblogs.com/wuyongyin/p/13628812.html
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比