OkHttp
官方文档 仓库
OkHttp是一款高效的HTTP客户端框架:
- HTTP/2支持允许对同一主机的所有请求共享一个套接字。
- 连接池可以减少请求延迟(如果HTTP/2不可用)。
- 透明GZIP压缩下载大小。
- 响应缓存完全避免了重复请求的网络。
当网络出现问题时,OkHttp会自动恢复一般的连接问题;若服务有多个IP地址,当第一个IP请求失败时,OkHttp会交替尝试你配置的其他IP。
OkHttp采用流式构造接口,方便调用;同时支持同步与异步方式调用。
依赖包
OkHttp3是OkHttp发展到版本3.0之后的名字。
要使用OkHttp,需要先在pom.xml中引入依赖包;okhttp3是使用kotlin实现的,所以对应包也需要引入:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.5.0</version>
</dependency>
创建OkHttpClient
通过共享的响应缓存/线程池/复用的连接等,绝大多数应用只需一个OkHttpClient实例,便可以满足整个应用的所有Http请求。
OkHttpClient client = new OkHttpClient();
OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS) // default 10s
.writeTimeout(30, TimeUnit.SECONDS) // default 10s
.readTimeout(30, TimeUnit.SECONDS) // default 10s
.build();
OkHttpClient anotherClient = client.newBuilder().build();
构造Url
通过HttpUrl.Builder可方便地构造Url:
-
addPathSegment
:添加路径; -
addQueryParameter
:添加请求参数,允许重复; -
setQueryParameter
:设定请求参数,覆盖重复; -
username、password
:添加用户名、密码;
public static HttpUrl buildUrl(String url, Map<String, String> queryParam) {
HttpUrl.Builder builder = HttpUrl.parse(url).newBuilder();
queryParam.forEach((k, v) -> {
builder.addQueryParameter(k, v);
});
return builder.build();
}
构造完整Url的流程:
HttpUrl.Builder builder = new HttpUrl.Builder()
.host("127.0.0.1")
.port(8001)
.addPathSegment("seg1")
.addPathSegment("path2")
.username("user")
.password("pass")
.scheme("https")
.addQueryParameter("k1", "v1")
.addQueryParameter("k1", "v2")
.setQueryParameter("uk", "v1")
.setQueryParameter("uk", "v2");
HttpUrl http = builder.build();
System.out.println(http.toString());
// https://user:pass@127.0.0.1:8001/seg1/path2?k1=v1&k1=v2&uk=v2
Header头设定
HTTP头(可参见请求头大全)是 Map<String, List<String>>
类型。也就是说,对于每个 HTTP 头,可能有多个值;但是大部分 HTTP 头都只有一个值。
OkHttp中通过Request构造时添加:
-
header(name,value)
:设置HTTP头的唯一值,若请求已经存在则替换掉。 -
addHeader(name,value)
:添加新值,若请求头中已经存在此name还会继续添加(此时,请求头中便会存在多个name相同而value不同的“键值对”)。 -
header(name)
:读取唯一值或多个值的最后一个值 -
headers(name)
:获取所有值
构造Request
时,必须设定Url
,默认是GET
模式:
Request request = new Request.Builder()
.url("http://127.0.0.1")
.addHeader("h1", "v1")
.addHeader("h1", "v2")
.header("uh", "v1")
.header("uh", "v2")
.build();
System.out.println(request.toString());
// Request{method=GET, url=http://127.0.0.1/, headers=[h1:v1, h1:v2, uh:v2]}
请求示例
使用OkHttp,需要:
- 构造
OkHttpClient
对象; - 构造
Request
对象; - 构造
Call
对象; - 发起请求:
execute
同步;enqueue
异步; - 返回
Response
:不可修改的Http相应对象。
Get请求
Request
默认就是Get
请求,所以构造时可以省略Get
;Get
请求参数通过queryParameter
添加。
同步Get
通过newCall
会同步调用,其返回内容可通过body
来获取;
private static void getSync() throws IOException {
String url = "http://wwww.baidu.com";
Request request = new Request.Builder()
.url(url) // 可以字符串,也可以是HttpUrl
.build();
Call call = httpClient.newCall(request);
Response resp = call.execute();
if (resp.code() == 200) {
System.out.println("Response: " + resp.body().string());
} else {
// Error handle
System.out.println("Code:" + resp.code() + ", Msg:" + resp.message());
}
}
异步Get
通过enqueue
,可提交异步请求;请求的应答通过回调Callback
返回。
private static void getAsync() {
HttpUrl url = buildUrl("http://127.0.0.1:7087/study/test/testEvent",
new HashMap<String, String>(){{
put("msg", "Test Msg event");
}});
// http://127.0.0.1:7087/study/test/testEvent?msg=Test%20Msg%20event
Request request = new Request.Builder()
.url(url)
.get() //默认就是GET请求,可以不写
.build();
Call call = httpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("Fail: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response resp) throws IOException {
System.out.println("onResponse: " + resp.body().string());
}
});
}
Post请求
Request
中通过Post
来标识Post
请求,并设定Post
的Body
内容。
Json内容
Json
是常用的序列化方式,只需把要传递的对象序列化为Json
字符串,然后以字符串Body
的方式传递到服务端。
private static void postJsonBody() throws IOException {
MediaType mediaType = MediaType.parse("application/json;charset=UTF-8");
RequestBody reqBody = RequestBody.create("msg for test", mediaType);
Request request = new Request.Builder()
.url("http://127.0.0.1:7087/study/test/postMsg")
.post(reqBody)
.build();
Response resp = httpClient.newCall(request).execute();
System.out.println("Response: " + resp.body().string());
}
Form表单
Form
表单都是以键值对的方式传递内容到服务端的,通过FormBody
可方便地构造。
private static void postFormBody() throws IOException {
RequestBody reqBody = new FormBody.Builder()
.add("msg", "form test")
.add("others", "other params")
.build();
Request request = new Request.Builder()
.url("http://127.0.0.1:7087/study/test/formMsg")
.post(reqBody)
.build();
Response resp = httpClient.newCall(request).execute();
System.out.println("Response: " + resp.body().string());
}
文件上传
上传文件时,使用MultipartBody
,并可通过MediaType
来设定媒体类型:
private static void uploadOneFile() throws IOException {
File file = new File("D:\\tmp\\Python-learning.md");
MediaType mediaType = MediaType.parse("application/octet-stream"); //设置类型为八位字节流
RequestBody reqBody = RequestBody.create(file, mediaType);
MultipartBody multiBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("id", "0") // for test
.addFormDataPart("file", file.getName(), reqBody)
.build();
Request request = new Request.Builder()
// .header("Authorization", "Bearer ****************") //添加请求头的身份认证Token
.url("http://127.0.0.1:7087/study/test/uploadFile")
.post(multiBody)
.build();
Response resp = httpClient.newCall(request).execute();
System.out.println("Response: " + resp.body().string());
}
要上传多个文件,就需要构造多份文件相关的RequestBody
,然后依次添加到MultipartBody
中:
private static void uploadMultiFile() throws IOException {
List<String> lstFile = Lists.newArrayList("D:\\tmp\\Python-learning.md", "D:\\tmp\\WebRTC-Learning.md");
MediaType mediaType = MediaType.parse("application/octet-stream"); //设置类型为八位字节流
MultipartBody.Builder multiBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM);
for(String f : lstFile){
File file = new File(f);
RequestBody fileBody = RequestBody.create(file, mediaType);
multiBody.addFormDataPart("file", file.getName(), fileBody);
}
Request request = new Request.Builder()
.url("http://127.0.0.1:7087/study/test/uploadMultiFile")
.post(multiBody.build())
.build();
Response resp = httpClient.newCall(request).execute();
System.out.println("Response: " + resp.body().string());
}
Put请求
Put
与Post
类似,只是Request
时用Put
标识。
Json内容
以传递Json
格式的Body
为例:
MediaType mediaType = MediaType.parse("application/json;charset=UTF-8");
RequestBody reqBody = RequestBody.create("{\"page\":0,\"query\":\"info to query\",\"size\":0}", mediaType);
Request request = new Request.Builder()
.url("http://127.0.0.1:7087/study/test/testPut")
.put(reqBody)
.build();
Response resp = httpClient.newCall(request).execute();
System.out.println("Response: " + resp.body().string());
参数方式
通过参数来传递请求的内容;但是Put
要求必须传递Body
,此时可构造一个空Body
:
HttpUrl url = buildUrl("http://127.0.0.1:7087/study/test/paramPut",
new HashMap<String, String>() {{
put("msg", "Test Msg event");
put("others", "other params");
}});
RequestBody reqBody = RequestBody.create(new byte[]{}, null);
Request request = new Request.Builder()
.url(url)
.put(reqBody)
.build();
Response resp = httpClient.newCall(request).execute();
System.out.println("Response: " + resp.body().string());
Delete请求
Request中通过Delete来标识请求,默认是通过参数方式的,也可通过RequestBody来传递。
HttpUrl url = buildUrl("http://127.0.0.1:7087/study/test/testDelete",
new HashMap<String, String>() {{
put("id", "delId");
}});
Request request = new Request.Builder()
.url(url)
.delete()
.build();
Response resp = httpClient.newCall(request).execute();
System.out.println("Response: " + resp.body().string());
OkHttp工具类
下面是一个封装的工具类,里边包含了日常用到的一些请求方式 和 日志打印 http异常捕获,可以根据实际需要进行裁剪使用
HttpUtil.java
package org.example.utils;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.example.exception.HttpStatusException;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import java.io.File;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Slf4j
public class HttpUtil {
private static OkHttpClient client;
private static final String DEFAULT_MEDIA_TYPE = "application/json; charset=utf-8";
private static final int CONNECT_TIMEOUT = 5;
private static final int READ_TIMEOUT = 7;
private static final String GET = "GET";
private static final String POST = "POST";
/**
* 单例模式 获取类实例
*
* @author: yh
* @date: 2022/12/1
*/
private static OkHttpClient getInstance() {
if (client == null) {
synchronized (OkHttpClient.class) {
if (client == null) {
client = new OkHttpClient.Builder()
// 设置默认连接超时时间。值为0表示没有超时,否则值必须在1和Integer之间
.connectTimeout(60, TimeUnit.SECONDS)
// 设置新连接的默认写超时时间。值为0表示没有超时,否则值必须在1和Integer之间
.writeTimeout(30, TimeUnit.SECONDS)
// 设置新连接的默认读超时时间。值为0表示没有超时,否则值必须在1和Integer之间
.readTimeout(30, TimeUnit.SECONDS)
.build();
}
}
}
return client;
}
/**
* 构造 Url
*
* @param url 请求路径
* @param queryParam 参数
* @return HttpUrl
*/
public static HttpUrl buildUrl(String url, Map<String, String> queryParam) {
HttpUrl.Builder builder = HttpUrl.parse(url).newBuilder();
// addPathSegment:添加路径;
// addQueryParameter:添加请求参数,允许重复;
// setQueryParameter:设定请求参数,覆盖重复;
// username、password:添加用户名、密码;
queryParam.forEach(builder::addQueryParameter);
return builder.build();
}
/**
* 普通 GET 请求
*
* @param url 请求路径
* @return response
* @author: yh
* @date: 2022/12/1
*/
public static String doGet(String url) {
try {
long startTime = System.currentTimeMillis();
// addRequestLog(GET, url, null, null);
Request request = new Request.Builder().url(url).build();
// 创建一个请求
Response response = getInstance().newCall(request).execute();
int httpCode = response.code();
String result;
ResponseBody body = response.body();
if (body != null) {
result = body.string();
// addResponseLog(httpCode, result, startTime);
} else {
throw new RuntimeException("exception in OkHttpUtil,response body is null");
}
return handleHttpResponse(httpCode, result);
} catch (Exception ex) {
handleHttpThrowable(ex, url);
return StringUtils.EMPTY;
}
}
/**
* 携带 HEADER 的 GET 请求
*
* @param url 请求路径
* @param headers 请求头
* @return response
* @author: yh
* @date: 2022/12/1
*/
public static String doGet(String url, Headers headers) {
try {
long startTime = System.currentTimeMillis();
// addRequestLog(GET, url, null, null);
Request request = new Request.Builder().url(url).headers(headers).build();
// 创建一个请求
Response response = getInstance().newCall(request).execute();
int httpCode = response.code();
String result;
ResponseBody body = response.body();
if (body != null) {
result = body.string();
// addResponseLog(httpCode, result, startTime);
} else {
throw new RuntimeException("exception in OkHttpUtil,response body is null");
}
return handleHttpResponse(httpCode, result);
} catch (Exception ex) {
handleHttpThrowable(ex, url);
return StringUtils.EMPTY;
}
}
/**
* POST 普通请求
*
* @param url 请求路径
* @param postBody 参数
* @param mediaType mediaType 默认:json
* @return response
* @author: yh
* @date: 2022/12/1
*/
public static String doPost(String url, String postBody, String mediaType){
try {
long startTime = System.currentTimeMillis();
// addRequestLog(POST, url, postBody, null);
MediaType createMediaType = MediaType.parse(mediaType == null ? DEFAULT_MEDIA_TYPE : mediaType);
Request request = new Request.Builder()
.url(url)
.post(RequestBody.create(createMediaType, postBody))
.build();
Response response = getInstance().newCall(request).execute();
int httpCode = response.code();
String result;
ResponseBody body = response.body();
if (body != null) {
result = body.string();
// addResponseLog(httpCode, result, startTime);
} else {
throw new IOException("exception in OkHttpUtil,response body is null");
}
return handleHttpResponse(httpCode, result);
} catch (Exception ex) {
handleHttpThrowable(ex, url);
return StringUtils.EMPTY;
}
}
/**
* POST 表单提交
*
* @param url 请求路径
* @param parameterMap 请求参数
* @return response
* @author: yh
* @date: 2022/12/1
*/
public static String doPost(String url, Map<String, String> parameterMap) {
try {
long startTime = System.currentTimeMillis();
// List<String> parameterList = new ArrayList<>();
FormBody.Builder builder = new FormBody.Builder();
if (parameterMap.size() > 0) {
for (Map.Entry<String, String> entry : parameterMap.entrySet()) {
String parameterName = entry.getKey();
String value = entry.getValue();
builder.add(parameterName, value);
// parameterList.add(parameterName + ":" + value);
}
}
// addRequestLog(POST, url, null, Arrays.toString(parameterList.toArray()));
FormBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Response response = getInstance().newCall(request).execute();
String result;
int httpCode = response.code();
ResponseBody body = response.body();
if (body != null) {
result = body.string();
addResponseLog(httpCode, result, startTime);
} else {
throw new IOException("exception in OkHttpUtil,response body is null");
}
return handleHttpResponse(httpCode, result);
} catch (Exception ex) {
handleHttpThrowable(ex, url);
return StringUtils.EMPTY;
}
}
/**
* POST 上传单个文件
*
* @param url 请求路径
* @param file 文件
* @return response
* @author: yh
* @date: 2022/12/1
*/
public static String uploadFile(String url, File file) {
try {
//设置类型为八位字节流
MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody reqBody = RequestBody.create(file, mediaType);
MultipartBody multiBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
// 设置参数
.addFormDataPart("id", "0")
.addFormDataPart("file", file.getName(), reqBody)
.build();
Request request = new Request.Builder()
.url(url)
.post(multiBody)
.build();
Response response = getInstance().newCall(request).execute();
String result;
int httpCode = response.code();
ResponseBody body = response.body();
if (body != null) {
result = body.string();
} else {
throw new IOException("exception in OkHttpUtil,response body is null");
}
return handleHttpResponse(httpCode, result);
} catch (Exception ex) {
handleHttpThrowable(ex, url);
return StringUtils.EMPTY;
}
}
/**
* POST 上传多个文件
*
* @param url 请求路径
* @param files 文件集合
* @return response
* @author: yh
* @date: 2022/12/1
*/
public static String uploadMultiFile(String url, List<File> files) {
try {
//设置类型为八位字节流
MediaType mediaType = MediaType.parse("application/octet-stream");
MultipartBody.Builder multiBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM);
for (File file : files) {
RequestBody fileBody = RequestBody.create(file, mediaType);
multiBody.addFormDataPart("file", file.getName(), fileBody);
}
Request request = new Request.Builder()
.url(url)
.post(multiBody.build())
.build();
Response response = getInstance().newCall(request).execute();
String result;
int httpCode = response.code();
ResponseBody body = response.body();
if (body != null) {
result = body.string();
} else {
throw new IOException("exception in OkHttpUtil,response body is null");
}
return handleHttpResponse(httpCode, result);
} catch (Exception ex) {
handleHttpThrowable(ex, url);
return StringUtils.EMPTY;
}
}
/**
* PUT 提交
*
* @param url 请求路径
* @param jsonParam 请求参数
* @return response
* @author: yh
* @date: 2022/12/1
*/
public static String doPut(String url, String jsonParam) {
try {
MediaType mediaType = MediaType.parse("application/json;charset=UTF-8");
RequestBody reqBody = RequestBody.create(jsonParam, mediaType);
Request request = new Request.Builder()
.url(url)
.put(reqBody)
.build();
Response response = getInstance().newCall(request).execute();
String result;
int httpCode = response.code();
ResponseBody body = response.body();
if (body != null) {
result = body.string();
} else {
throw new IOException("exception in OkHttpUtil,response body is null");
}
return handleHttpResponse(httpCode, result);
} catch (Exception ex) {
handleHttpThrowable(ex, url);
return StringUtils.EMPTY;
}
}
/**
* PUT 提交
*
* @param url 请求路径
* @param paramMap 请求参数 Map类型
* @return response
* @author: yh
* @date: 2022/12/1
*/
public static String doPut(String url, Map<String, String> paramMap) {
try {
HttpUrl httpUrl = buildUrl(url, paramMap);
RequestBody reqBody = RequestBody.create(new byte[]{}, null);
Request request = new Request.Builder()
.url(httpUrl)
.put(reqBody)
.build();
Response response = getInstance().newCall(request).execute();
String result;
int httpCode = response.code();
ResponseBody body = response.body();
if (body != null) {
result = body.string();
} else {
throw new IOException("exception in OkHttpUtil,response body is null");
}
return handleHttpResponse(httpCode, result);
} catch (Exception ex) {
handleHttpThrowable(ex, url);
return StringUtils.EMPTY;
}
}
/**
* DELETE 请求
*
* @param url 请求路径
* @param paramMap 请求参数 Map类型
* @return response
* @author: yh
* @date: 2022/12/1
*/
public static String doDelete(String url, Map<String, String> paramMap) {
try {
HttpUrl httpUrl = buildUrl(url, paramMap);
Request request = new Request.Builder()
.url(httpUrl)
.delete()
.build();
Response response = getInstance().newCall(request).execute();
String result;
int httpCode = response.code();
ResponseBody body = response.body();
if (body != null) {
result = body.string();
} else {
throw new IOException("exception in OkHttpUtil,response body is null");
}
return handleHttpResponse(httpCode, result);
} catch (Exception ex) {
handleHttpThrowable(ex, url);
return StringUtils.EMPTY;
}
}
/**
* 请求前记录日志
*
* @param method
* @param url
* @param body
* @param formParam
* @author: yh
* @date: 2022/12/1
*/
private static void addRequestLog(String method, String url, String body, String formParam) {
log.info("===========================request begin================================================");
log.info("URI : {}", url);
log.info("Method : {}", method);
if (StringUtils.isNotBlank(body)) {
log.info("Request body : {}", body);
}
if (StringUtils.isNotBlank(formParam)) {
log.info("Request param: {}", formParam);
}
log.info("---------------------------request end--------------------------------------------------");
}
/**
* 请求成功响应日志
*
* @param httpCode 状态码
* @param result 请求结果
* @param startTime 请求前计时
* @author: yh
* @date: 2022/12/1
*/
private static void addResponseLog(int httpCode, String result, long startTime) {
long endTime = System.currentTimeMillis();
log.info("Status : {}", httpCode);
log.info("Response : {}", result);
log.info("Time : {} ms", endTime - startTime);
log.info("===========================response end================================================");
}
/**
* 状态码不为 200 抛出异常
*
* @param httpCode 响应状态码
* @param result 响应结果
* @return 响应结果
* @author: yh
* @date: 2022/12/1
*/
private static String handleHttpResponse(int httpCode, String result) throws HttpStatusException {
if (httpCode == HttpStatus.OK.value()) {
return result;
}
HttpStatus httpStatus = HttpStatus.valueOf(httpCode);
throw new HttpStatusException(httpStatus);
}
/**
* 打印 异常信息
*
* @param ex 异常
* @param url 请求的路径
* @author: yh
* @date: 2022/12/1
*/
private static void handleHttpThrowable(Exception ex, String url) {
if (ex instanceof HttpStatusException) {
// throw (HttpStatusException) ex;
log.error("OkHttp exception: " + ex.getMessage());
}
log.error("OkHttp error url: " + url, ex);
if (ex instanceof SocketTimeoutException) {
throw new RuntimeException("request time out of OkHttp when do url:" + url);
}
throw new RuntimeException(ex);
}
}
Http请求异常类:HttpStatusException.java
package org.example.exception;
import org.springframework.http.HttpStatus;
/**
* 异常类
* @author: yh
* @date: 2022/12/1
*/
public class HttpStatusException extends Exception {
private HttpStatus httpStatus;
private String result;
public HttpStatusException(HttpStatus httpStatus) {
this.httpStatus = httpStatus;
}
public HttpStatusException(HttpStatus httpStatus, String result) {
this.httpStatus = httpStatus;
}
public HttpStatus getHttpStatus() {
return httpStatus;
}
public String getResult() {
return result;
}
}
调用示例:文章来源:https://www.toymoban.com/news/detail-432003.html
// GET 请求携带请求头
Headers headers = new Headers.Builder().add("token", "123456").build();
String s = HttpUtil.doGet("https://127.0.0.1/test", headers);
System.out.println(s);
// ......
到此,本章内容就介绍完啦,如果有帮助到你 欢迎点个赞👍👍吧!!您的鼓励是博主的最大动力! 有问题评论区交流。文章来源地址https://www.toymoban.com/news/detail-432003.html
到了这里,关于OkHttp搞定Http请求的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!