精品熟女碰碰人人a久久,多姿,欧美欧美a v日韩中文字幕,日本福利片秋霞国产午夜,欧美成人禁片在线观看

Java使用HttpUtils怎么實現發送HTTP請求

java使用httputils怎么實現發送http請求

本文講解"java使用httputils如何實現發送http請求",希望能夠解決相關問題。

目錄
  • 第一部分:簡單總結http請求常用配置

  • 第二部分:使用javalib的httputils

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

http請求,在日常開發中,還是比較常見的,今天給大家分享httputils如何使用。

閱讀本文,你將收獲:

  • 簡單總結http請求常用配置;

  • javalib中httputils如何使用;

  • 如何封裝http請求工具類。

第一部分:簡單總結http請求常用配置

大家好,在 java 開發中,經常遇到需要調用第三方提供的接口服務,常見的形式是 http + json,下面,就對 http 請求常見的設置,做一個說明

http提供多種請求方式,以滿足我們日常需要,先按請求方式來做說明:

  • get

  • post

  • put

  • patch

  • delete

在 restful api 開發中,我們可以根據這些請求方式設計我們的api接口。舉例:

  • get:獲取資源

  • post:提交資源

  • put:更新完整資源

  • patch:更新部分資源

  • delete:刪除資源

參數格式

  • form表單

  • json

其他

  • 超時時間設置

第二部分:使用javalib的httputils

簡單的get請求

system.out.println(httputils.get("https://www.baidu.com"));

響應結果:

<!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&amp;tpl=mn&amp;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?>更多產品</a>?</div>?</div>?</div>?<div?id=ftcon>?<div?id=ftconw>?<p?id=lh>?<a?href=http://home.baidu.com>關于百度</a>?<a?href=http://ir.baidu.com>about?baidu</a>?</p>?<p?id=cp>&copy;2017&nbsp;baidu&nbsp;<a?href=http://www.baidu.com/duty/>使用百度前必讀</a>&nbsp;?<a?href=http://jianyi.baidu.com/?class=cp-feedback>意見反饋</a>&nbsp;京icp證030173號&nbsp;?<img?src=//www.baidu.com/img/gs.gif>?</p>?</div>?</div>?</div>?</body>?</html>

通過簡單嘗試,證明兩點:

一是,這個還是可以使用的;二是,原來使用如此簡單。

如果要你手寫一個http請求,或許你腦海里一篇空白,會去搜索各種資料。

我們再試一個復雜的

接口:

接口地址:https://erwin-api.fengwenyi.com/erwin/blog/page請求方法:get
參數:currentpage,pagesize
參數格式:form
響應:application/json

java使用httputils怎么實現發送http請求

java使用httputils怎么實現發送http請求

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);
}

響應結果:

{
????"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?中文顯示出現黃色方框的解決方法_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實現導航欄?-?掘金",
????????????????"url":"https://juejin.cn/post/7084871748608327687",
????????????????"icon":null,
????????????????"classifyname":"前端",
????????????????"classifyid":"1525554881275990018"
????????????},
????????????{
????????????????"id":"1631593154401636354",
????????????????"timestamp":1677837096000,
????????????????"enabledstate":null,
????????????????"name":"spring?bean?創建過程源碼解析?-?騰訊云開發者社區-騰訊云",
????????????????"url":"https://cloud.tencent.com/developer/article/1631160",
????????????????"icon":null,
????????????????"classifyname":"spring",
????????????????"classifyid":"1522586360887742466"
????????????},
????????????{
????????????????"id":"1631592987673858050",
????????????????"timestamp":1677837056000,
????????????????"enabledstate":null,
????????????????"name":"springboot之容器啟動源碼分析與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的生命周期_老周聊架構的博客-csdn博客",
????????????????"url":"https://blog.csdn.net/riemann_/article/details/118500805",
????????????????"icon":null,
????????????????"classifyname":"spring",
????????????????"classifyid":"1522586360887742466"
????????????},
????????????{
????????????????"id":"1630768897186697218",
????????????????"timestamp":1677640578000,
????????????????"enabledstate":null,
????????????????"name":"mysql同時統計多個條件的記錄條數_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查詢近七天,近兩周,近一個月的數據_sql最近一周數據_心誠則靈'的博客-csdn博客",
????????????????"url":"https://blog.csdn.net/wenchangwenliu/article/details/119891790",
????????????????"icon":null,
????????????????"classifyname":"mysql",
????????????????"classifyid":"1522586805693681666"
????????????},
????????????{
????????????????"id":"1630480535938764801",
????????????????"timestamp":1677571827000,
????????????????"enabledstate":null,
????????????????"name":"開源流程引擎哪個好,如何選型??-?知乎",
????????????????"url":"https://zhuanlan.zhihu.com/p/369761832",
????????????????"icon":null,
????????????????"classifyname":"java",
????????????????"classifyid":"1522586296119300097"
????????????}
????????]
????}
}

響應結果,還是符合預期的。

當然,http還有其他,比如超時等等,下面看看完整版的請求示例:

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);
}

響應結果也是跟上面一樣的。

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

首先是 request 和 response 對應http的請求和響應,包路徑如下:

  • com.fengwenyi.javalib.http.request

  • com.fengwenyi.javalib.http.response

另外,com.fengwenyi.javalib.http.request.option 來存放http參數配置。

這一部分的思路來源是 spring cloud openfeign。

為了兼容多種http工具實現請求,引入了 httpclientfactory,其他工具類,只要實現 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());
????}
}

所以,只需要實現 httpclient#execute 接口就行。

response?execute(request?request,?request.option?option)?throws?ioexception;

關于 "java使用httputils如何實現發送http請求" 就介紹到此。希望多多支持碩編程。

下一節:關于spring的統一功能處理(攔截器)實現

java編程技術

相關文章