在Java的世界中,Http客户端之前一直是Apache家的HttpClient占据主导,但是由于此包较为庞大,API又比较难用,因此并不使用很多场景。而新兴的OkHttp、Jodd-http固然好用,但是面对一些场景时,学习成本还是有一些的。很多时候,我们想追求轻量级的Http客户端,并且追求简单易用。而OKHttp
是一套处理 HTTP 网络请求的依赖库,由 Square 公司设计研发并开源,目前可以在 Java 和 Kotlin 中使用。对于 Android App
来说,OkHttp 现在几乎已经占据了所有的网络请求操作,对于服务器端请求外部接口也是必备的选择 。针对OKHttp
OkHttpUtil做了一层封装,使Http请求变得无比简单。
maven引入
<dependency>
<groupId>io.github.admin4jgroupId>
<artifactId>httpartifactId>
<version>0.4.0version>
dependency>
最新版查询 https://search.maven.org/artifact/io.github.admin4j/http
最简单的使用莫过于用HttpUtil工具类快速请求某个接口&#xff1a;
Response response &#61; HttpUtil.get("https://github.com/search", Pair.of("q", "okhttp"));
System.out.println("response &#61; " &#43; response);
一行代码即可搞定&#xff0c;当然Post请求也很简单&#xff1a;
# JSON 格式的body
Response post &#61; HttpUtil.post("https://oapi.dingtalk.com/robot/send?access_token&#61;27f5954ab60ea8b2e431ae9101b1289c138e85aa6eb6e3940c35ee13ff8b6335", "{\"msgtype\": \"text\",\"text\": {\"content\":\"【反馈提醒】我就是我, 是不一样的烟火\"}}");
System.out.println("post &#61; " &#43; post);
# form 请求
Map
formParams.put("username", "admin");
formParams.put("password", "admin123");
Response response &#61; HttpUtil.postForm("http://192.168.1.13:9100/auth/login",
formParams
);
System.out.println("response &#61; " &#43; response);
返回格式为JSON的 可以使用 HttpJsonUtil 自动返回JsonObject
JSONObject object&#61;HttpJsonUtil.get("https://github.com/search",
Pair.of("q","http"),
Pair.of("username","agonie201218"));
System.out.println("object &#61; "&#43;object);
File file&#61;new File("C:\\Users\\andanyang\\Downloads\\Sql.txt");
Map<String, Object> formParams&#61;new HashMap<>();
formParams.put("key","test");
formParams.put("file",file);
formParams.put("token","WXyUseb-D4sCum-EvTIDYL-mEehwDtrSBg-Zca7t:qgOcR2gUoKmxt-VnsNb657Oatzo&#61;:eyJzY29wZSI6InpoYW56aGkiLCJkZWFkbGluZSI6MTY2NTMwNzUxNH0&#61;");
Response response&#61;HttpUtil.upload("https://upload.qiniup.com/",formParams);
System.out.println(response);
HttpUtil.down("https://gitee.com/admin4j/common-http","path/");
# get
Response response&#61;HttpRequest.get("https://search.gitee.com/?skin&#61;rec&type&#61;repository")
.queryMap("q","admin4j")
.header(HttpHeaderKey.USER_AGENT,"admin4j")
.execute();
System.out.println("response &#61; "&#43;response);
# post form
Response response&#61;HttpRequest.get("http://192.168.1.13:9100/auth/login")
.queryMap("q","admin4j")
.header(HttpHeaderKey.USER_AGENT,"admin4j")
.form("username","admin")
.form("password","admin123")
.execute();
System.out.println("response &#61; "&#43;response);
post form 日志
16:49:14.092[main]DEBUG io.github.admin4j.http.core.HttpLogger- -->GET http://192.168.1.13:9100/auth/login?q&#61;admin4j http/1.1
16:49:14.094[main]DEBUG io.github.admin4j.http.core.HttpLogger-User-Agent:admin4j
16:49:14.094[main]DEBUG io.github.admin4j.http.core.HttpLogger-Host:192.168.1.13:9100
16:49:14.094[main]DEBUG io.github.admin4j.http.core.HttpLogger-Connection:Keep-Alive
16:49:14.094[main]DEBUG io.github.admin4j.http.core.HttpLogger-Accept-Encoding:gzip
16:49:14.094[main]DEBUG io.github.admin4j.http.core.HttpLogger- -->END GET
16:49:14.670[main]DEBUG io.github.admin4j.http.core.HttpLogger-<--200OK http://192.168.1.13:9100/auth/login?q&#61;admin4j (575ms)
16:49:14.670[main]DEBUG io.github.admin4j.http.core.HttpLogger-transfer-encoding:chunked
16:49:14.670[main]DEBUG io.github.admin4j.http.core.HttpLogger-Vary:Origin
16:49:14.670[main]DEBUG io.github.admin4j.http.core.HttpLogger-Vary:Access-Control-Request-Method
16:49:14.670[main]DEBUG io.github.admin4j.http.core.HttpLogger-Vary:Access-Control-Request-Headers
16:49:14.670[main]DEBUG io.github.admin4j.http.core.HttpLogger-Vary:Origin
16:49:14.670[main]DEBUG io.github.admin4j.http.core.HttpLogger-Vary:Access-Control-Request-Method
16:49:14.670[main]DEBUG io.github.admin4j.http.core.HttpLogger-Vary:Access-Control-Request-Headers
16:49:14.671[main]DEBUG io.github.admin4j.http.core.HttpLogger-Content-Type:application/json;charset&#61;utf-8
16:49:14.671[main]DEBUG io.github.admin4j.http.core.HttpLogger-Date:Tue,08Nov 2022 08:49:14GMT
16:49:14.671[main]DEBUG io.github.admin4j.http.core.HttpLogger-
16:49:14.671[main]DEBUG io.github.admin4j.http.core.HttpLogger-{"code":406,"msg":"Full authentication is required to access this resource"}
16:49:14.671[main]DEBUG io.github.admin4j.http.core.HttpLogger-<--END HTTP(76-byte body)
response&#61;Response{protocol&#61;http/1.1,code&#61;200,message&#61;OK,url&#61;http://192.168.1.13:9100/auth/login?q&#61;admin4j}
再 Springboot 中使用
maven引入
<dependency>
<groupId>io.github.admin4jgroupId>
<artifactId>common-http-starterartifactId>
<version>0.4.0version>
dependency>
最新版查询 io.github.admin4j:common-http-starter
spring 版可以对 OkHttp进行个性化配置
配置详见
public class HttpConfig {
/**
* 日志等级
*/
private HttpLoggingInterceptor.Level loggLevel &#61; HttpLoggingInterceptor.Level.BODY;
/**
* 读取超时时间&#xff0c;秒
*/
private long readTimeout &#61; 30;
/**
* 链接超时时间
*/
private long connectTimeout &#61; 30;
private boolean followRedirects &#61; false;
/**
* 最大的连接数
*/
private int maxIdleConnections &#61; 5;
/**
* 最大的kepAlive 时间 秒
*/
private long keepAliveDuration &#61; 5;
private String userAgent &#61; "OKHTTP";
/**
* 是否支持COOKIE
*/
private boolean COOKIE &#61; false;
private ProxyConfig proxy;
&#64;Data
public static class ProxyConfig {
private Proxy.Type type &#61; Proxy.Type.HTTP;
private String host;
private Integer port &#61; 80;
private String userName;
private String password;
}
}
如何快速封装外部接口
以实体项目为例&#xff0c;封装 ebay接口
public class EbayClient extends ApiJsonClient {
/**
* 店铺配置
*
* &#64;param storeId
*/
public EbayClient(Long storeId) {
//TODO 获取店铺相关配置
Map<String, String> config &#61; new HashMap<>();
basePath &#61; "https://api.ebay.com";
defaultHeaderMap.put("Authorization", "Bearer " &#43; config.get("accessToken"));
defaultHeaderMap.put("X-EBAY-C-MARKETPLACE-ID", config.get("marketplaceId"));
}
}
EbayClient 封装ebay api请求 基础类
/**
* ebay 库存相关api
* &#64;author andanyang
*/
public class EbayInventoryClient extends EbayClient {
/**
* 店铺配置
*
* &#64;param storeId
*/
public EbayInventoryClient(Long storeId) {
super(storeId);
}
/**
* 库存列表
*
* &#64;param limit
* &#64;param offset
* &#64;return
* &#64;throws IOException
*/
public JSONObject inventoryItem(Integer limit, Integer offset) throws IOException {
Map<String, Object> queryMap &#61; new HashMap(2);
queryMap.put("limit", limit);
queryMap.put("offset", offset);
return get("/sell/inventory/v1/inventory_item", queryMap);
}
}
EbayInventoryClient 封装ebay 库存 api请求
使用
EbayInventoryClient ebayInventoryClient&#61;new EbayInventoryClient(1L);
JSONObject jsonObject&#61;ebayInventoryClient.inventoryItem(0,10);
/**
* 订单相关api
* &#64;author andanyang
*/
public class EbayOrderClient extends EbayClient {
/**
* 店铺配置
*
* &#64;param storeId
*/
public EbayOrderClient(Long storeId) {
super(storeId);
}
/**
* 订单列表
*
* &#64;param beginTime
* &#64;param endTime
* &#64;param limit
* &#64;param offset
* &#64;return
*/
public JSONObject orders(String beginTime, String endTime, int limit, int offset) {
final String path &#61; "/sell/fulfillment/v1/order";
String filter &#61; MessageFormat.format("lastmodifieddate:[{0}..{1}]", beginTime, endTime);
//
Map<String, Object> queryMap &#61; new HashMap<>(8);
queryMap.put("filter", filter);
queryMap.put("limit", limit);
queryMap.put("offset", offset);
return get("/sell/inventory/v1/inventory_item", queryMap);
}
}
库存相关的使用EbayInventoryClient
,订单相关的使用EbayOrderClient
,是不是很清晰