最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

在Java中,使用HttpUtils實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求

2023-03-12 18:21 作者:Erwin_Feng  | 我要投稿

微信公眾號(hào):馮文議(ID:fwy-world)

HTTP請(qǐng)求,在日常開發(fā)中,還是比較常見的,今天給大家分享HttpUtils如何使用。

閱讀本文,你將收獲:

  • 簡(jiǎn)單總結(jié)HTTP請(qǐng)求常用配置;

  • JavaLib中HttpUtils如何使用;

  • 如何封裝HTTP請(qǐng)求工具類。

第一部分:簡(jiǎn)單總結(jié)HTTP請(qǐng)求常用配置

大家好,在 Java 開發(fā)中,經(jīng)常遇到需要調(diào)用第三方提供的接口服務(wù),常見的形式是 HTTP + JSON,下面,就對(duì) http 請(qǐng)求常見的設(shè)置,做一個(gè)說明

http提供多種請(qǐng)求方式,以滿足我們?nèi)粘P枰劝凑?qǐng)求方式來做說明:

  • GET

  • POST

  • PUT

  • PATCH

  • DELETE

在 RESTful API 開發(fā)中,我們可以根據(jù)這些請(qǐng)求方式設(shè)計(jì)我們的API接口。舉例:

  • GET:獲取資源

  • POST:提交資源

  • PUT:更新完整資源

  • PATCH:更新部分資源

  • DELETE:刪除資源

參數(shù)格式

  • form表單

  • json

其他

  • 超時(shí)時(shí)間設(shè)置

第二部分:使用JavaLib的HttpUtils

簡(jiǎn)單的get請(qǐng)求

System.out.println(HttpUtils.get("https://www.baidu.com"));

響應(yīng)結(jié)果:

baidu

通過簡(jiǎn)單嘗試,證明兩點(diǎn):

  • 一是,這個(gè)還是可以使用的;

  • 二是,原來使用如此簡(jiǎn)單。

如果要你手寫一個(gè)http請(qǐng)求,或許你腦海里一篇空白,會(huì)去搜索各種資料。

我們?cè)僭囈粋€(gè)復(fù)雜的

接口:

接口地址:https://erwin-api.fengwenyi.com/erwin/blog/page
請(qǐng)求方法:GET
參數(shù):currentPage,pageSize
參數(shù)格式:form
響應(yīng):application/json

博客列表分頁(yè)查詢接口

String?url?=?"https://erwin-api.fengwenyi.com/erwin/bookmark/page?currentPage=1&pageSize=10";

Request?request?=?new?Request();
request.setUrl(url);
request.setMethod(Request.Method.GET);

Map<String,?String>?headerMap?=?new?HashMap<>();
headerMap.put("Accept",?"application/json");

Request.Option?option?=?new?Request.Option();
option.setHeaders(headerMap);

try?{
????System.out.println(HttpUtils.execute(request,?option));
}?catch?(IOException?e)?{
????throw?new?RuntimeException(e);
}

響應(yīng)結(jié)果:

{
????"code":"SUCCESS",
????"message":"Success",
????"success":true,
????"header":null,
????"body":{
????????"currentPage":1,
????????"pageSize":10,
????????"totalRows":661,
????????"totalPages":67,
????????"content":[
????????????{
????????????????"id":"1634772578877935617",
????????????????"timestamp":1678595130000,
????????????????"enabledState":null,
????????????????"name":"VScode?中文顯示出現(xiàn)黃色方框的解決方法_vscode漢字被框住_YJer的博客-CSDN博客",
????????????????"url":"https://blog.csdn.net/qq_33249042/article/details/123252625",
????????????????"icon":null,
????????????????"classifyName":"軟件",
????????????????"classifyId":"1522587269600481281"
????????????},
????????????{
????????????????"id":"1632640455110922241",
????????????????"timestamp":1678086792000,
????????????????"enabledState":null,
????????????????"name":"Spring中init-method和destroy-method的四種方式_星夜孤帆的博客-CSDN博客",
????????????????"url":"https://blog.csdn.net/qq_38826019/article/details/117387398",
????????????????"icon":null,
????????????????"classifyName":"Spring",
????????????????"classifyId":"1522586360887742466"
????????????},
????????????{
????????????????"id":"1631597310596190209",
????????????????"timestamp":1677838087000,
????????????????"enabledState":null,
????????????????"name":"vue3?+?elemenplus實(shí)現(xiàn)導(dǎo)航欄?-?掘金",
????????????????"url":"https://juejin.cn/post/7084871748608327687",
????????????????"icon":null,
????????????????"classifyName":"前端",
????????????????"classifyId":"1525554881275990018"
????????????},
????????????{
????????????????"id":"1631593154401636354",
????????????????"timestamp":1677837096000,
????????????????"enabledState":null,
????????????????"name":"Spring?bean?創(chuàng)建過程源碼解析?-?騰訊云開發(fā)者社區(qū)-騰訊云",
????????????????"url":"https://cloud.tencent.com/developer/article/1631160",
????????????????"icon":null,
????????????????"classifyName":"Spring",
????????????????"classifyId":"1522586360887742466"
????????????},
????????????{
????????????????"id":"1631592987673858050",
????????????????"timestamp":1677837056000,
????????????????"enabledState":null,
????????????????"name":"SpringBoot之容器啟動(dòng)源碼分析與Bean加載_springboot加載bean?源碼_minemine0418的博客-CSDN博客",
????????????????"url":"https://blog.csdn.net/minemine0418/article/details/102308912",
????????????????"icon":null,
????????????????"classifyName":"Spring?Boot",
????????????????"classifyId":"1522586446766116865"
????????????},
????????????{
????????????????"id":"1631586585454678018",
????????????????"timestamp":1677835530000,
????????????????"enabledState":null,
????????????????"name":"Spring-Bean生命周期?-?知乎",
????????????????"url":"https://zhuanlan.zhihu.com/p/158468104",
????????????????"icon":null,
????????????????"classifyName":"Spring",
????????????????"classifyId":"1522586360887742466"
????????????},
????????????{
????????????????"id":"1631579732104548354",
????????????????"timestamp":1677833896000,
????????????????"enabledState":null,
????????????????"name":"一文讀懂?Spring?Bean?的生命周期_spring?bean的生命周期_老周聊架構(gòu)的博客-CSDN博客",
????????????????"url":"https://blog.csdn.net/riemann_/article/details/118500805",
????????????????"icon":null,
????????????????"classifyName":"Spring",
????????????????"classifyId":"1522586360887742466"
????????????},
????????????{
????????????????"id":"1630768897186697218",
????????????????"timestamp":1677640578000,
????????????????"enabledState":null,
????????????????"name":"MySQL同時(shí)統(tǒng)計(jì)多個(gè)條件的記錄條數(shù)_ztnhnr的博客-CSDN博客",
????????????????"url":"https://blog.csdn.net/ztnhnr/article/details/107165942",
????????????????"icon":null,
????????????????"classifyName":"MySQL",
????????????????"classifyId":"1522586805693681666"
????????????},
????????????{
????????????????"id":"1630768792098410497",
????????????????"timestamp":1677640553000,
????????????????"enabledState":null,
????????????????"name":"sql查詢近七天,近兩周,近一個(gè)月的數(shù)據(jù)_sql最近一周數(shù)據(jù)_心誠(chéng)則靈'的博客-CSDN博客",
????????????????"url":"https://blog.csdn.net/wenchangwenliu/article/details/119891790",
????????????????"icon":null,
????????????????"classifyName":"MySQL",
????????????????"classifyId":"1522586805693681666"
????????????},
????????????{
????????????????"id":"1630480535938764801",
????????????????"timestamp":1677571827000,
????????????????"enabledState":null,
????????????????"name":"開源流程引擎哪個(gè)好,如何選型??-?知乎",
????????????????"url":"https://zhuanlan.zhihu.com/p/369761832",
????????????????"icon":null,
????????????????"classifyName":"Java",
????????????????"classifyId":"1522586296119300097"
????????????}
????????]
????}
}

響應(yīng)結(jié)果,還是符合預(yù)期的。

當(dāng)然,HTTP還有其他,比如超時(shí)等等,下面看看完整版的請(qǐng)求示例:

Map<String,?Object>?paramMap?=?new?HashMap<>();
paramMap.put("currentPage",?1);
paramMap.put("pageSize",?10);

Map<String,?String>?headerMap?=?new?HashMap<>();
headerMap.put("Accept",?"application/json");

Request?request?=?new?Request();
request.setUrl("https://erwin-api.fengwenyi.com/erwin/bookmark/page");
request.setParam(paramMap);
request.setMethod(Request.Method.GET);
request.setUtil(Request.Util.OkHttp);

Request.Option?option?=?new?Request.Option();
option.setHeaders(headerMap);
option.setConnectTimeoutSecond(3);
option.setReadTimeoutSecond(5);
option.setLogLevel(Request.LogLevel.DEBUG);

try?{
????String?result?=?HttpUtils.execute(request,?option);
????System.out.println(result);
}?catch?(IOException?e)?{
????throw?new?RuntimeException(e);
}

響應(yīng)結(jié)果也是跟上面一樣的。

第三部分:分享HttpUtils是如何封裝的

首先是 RequestResponse 對(duì)應(yīng)HTTP的請(qǐng)求和響應(yīng),包路徑如下:

  • com.fengwenyi.javalib.http.Request

  • com.fengwenyi.javalib.http.Response

另外,com.fengwenyi.javalib.http.Request.Option 來存放HTTP參數(shù)配置。

這一部分的思路來源是 Spring Cloud OpenFeign。

為了兼容多種HTTP工具實(shí)現(xiàn)請(qǐng)求,引入了 HttpClientFactory,其他工具類,只要實(shí)現(xiàn) HttpClient 接口,就行。

  • com.fengwenyi.javalib.http.client.HttpClient

  • com.fengwenyi.javalib.http.client.HttpClientFactory

歐克,下面我們就以代碼來看看:

HttpUtils#execute

public?static?String?execute(Request?request,?Request.Option?option)?throws?IOException?{
????check(request);
????HttpClient?httpClient?=?HttpClientFactory.get(request.getUtil());
????Response?response?=?httpClient.execute(request,?option);
????return?handleResponse(response);
}

HttpClientFactory#get

public?static?HttpClient?get(Request.Util?httpUtil)?{
????if?(Request.Util.JDK?==?httpUtil)?{
????????return?new?JdkHttpClient();
????}?else?if?(Request.Util.OkHttp?==?httpUtil)?{
????????return?new?OkHttpClient();
????}?else?{
????????throw?new?RuntimeException("not?find?http?util:?"?+?httpUtil.name());
????}
}

所以,只需要實(shí)現(xiàn) HttpClient#execute 接口就行。

Response?execute(Request?request,?Request.Option?option)?throws?IOException;

源碼:https://github.com/fengwenyi/JavaLib

好了,今天的分享就到這里了。我是小馮,一名Java程序員,專注于程序設(shè)計(jì)和開發(fā),如果你在開發(fā)上遇到問題,歡迎一起交流,微信公眾號(hào):馮文議(ID:fwy-world)。


在Java中,使用HttpUtils實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
莱阳市| 胶州市| 九江市| 怀宁县| 开化县| 平顺县| 邯郸县| 和硕县| 吉隆县| 兴化市| 彰化市| 梁河县| 汉川市| 阿城市| 肇源县| 吴堡县| 内乡县| 正镶白旗| 杂多县| 正阳县| 铜山县| 察隅县| 定陶县| 柏乡县| 武威市| 三门县| 锦屏县| 句容市| 吴川市| 香港| 习水县| 启东市| 叶城县| 宿州市| 饶平县| 渭源县| 宣化县| 玉环县| 宁安市| 永泰县| 樟树市|