java使用httputils怎么實(shí)現(xiàn)發(fā)送http請(qǐng)求
本文講解"java使用httputils如何實(shí)現(xiàn)發(fā)送http請(qǐng)求",希望能夠解決相關(guān)問題。
目錄- 第一部分:簡(jiǎn)單總結(jié)http請(qǐng)求常用配置
- 第二部分:使用javalib的httputils
- 第三部分:分享httputils是如何封裝的
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é)果:
<!doctype?html><!--status?ok--><html>?<head><meta?http-equiv=content-type?content=text/html;charset=utf-8><meta?http-equiv=x-ua-compatible?content=ie=edge><meta?content=always?name=referrer><link?rel=stylesheet?type=text/css?href=https://ss1.bdstatic.com/5en1bjq8aauym2zgoy3k/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head>?<body?link=#0000cc>?<div?id=wrapper>?<div?id=head>?<div?class=head_wrapper>?<div?class=s_form>?<div?class=s_form_wrapper>?<div?id=lg>?<img?hidefocus=true?src=//www.baidu.com/img/bd_logo1.png?width=270?height=129>?</div>?<form?id=form?name=f?action=//www.baidu.com/s?class=fm>?<input?type=hidden?name=bdorz_come?value=1>?<input?type=hidden?name=ie?value=utf-8>?<input?type=hidden?name=f?value=8>?<input?type=hidden?name=rsv_bp?value=1>?<input?type=hidden?name=rsv_idx?value=1>?<input?type=hidden?name=tn?value=baidu><span?class="bg?s_ipt_wr"><input?id=kw?name=wd?class=s_ipt?value?maxlength=255?autocomplete=off?autofocus=autofocus><span?class="bg?s_btn_wr"><input?type=submit?id=su?value=百度一下?class="bg?s_btn"?autofocus>?</form>?</div>?</div>?<div?id=u1>?<a?href=http://news.baidu.com?name=tj_trnews?class=mnav>新聞</a>?<a?href=https://www.hao123.com?name=tj_trhao123?class=mnav>hao123</a>?<a?href=http://map.baidu.com?name=tj_trmap?class=mnav>地圖</a>?<a?href=http://v.baidu.com?name=tj_trvideo?class=mnav>視頻</a>?<a?href=http://tieba.baidu.com?name=tj_trtieba?class=mnav>貼吧</a>?<noscript>?<a?href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3a%2f%2fwww.baidu.com%2f%3fbdorz_come%3d1?name=tj_login?class=lb>登錄</a>?</noscript>?<script>document.write('<a?);????????????????</script>?<a?href=//www.baidu.com/more/?name=tj_briicon?class=bri?>更多產(chǎn)品</a>?</div>?</div>?</div>?<div?id=ftcon>?<div?id=ftconw>?<p?id=lh>?<a?href=http://home.baidu.com>關(guān)于百度</a>?<a?href=http://ir.baidu.com>about?baidu</a>?</p>?<p?id=cp>©2017 baidu <a?href=http://www.baidu.com/duty/>使用百度前必讀</a> ?<a?href=http://jianyi.baidu.com/?class=cp-feedback>意見反饋</a> 京icp證030173號(hào) ?<img?src=//www.baidu.com/img/gs.gif>?</p>?</div>?</div>?</div>?</body>?</html>
通過簡(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
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是如何封裝的
首先是 request 和 response 對(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;
關(guān)于 "java使用httputils如何實(shí)現(xiàn)發(fā)送http請(qǐng)求" 就介紹到此。希望多多支持碩編程。