In Java, use HttpUtils to implement sending HTTP requests

HTTP requests are relatively common in daily development. Today I will share with you how to use HttpUtils.

After reading this article, you will gain:

  • A brief summary of common configurations for HTTP requests;
  • How to use HttpUtils in JavaLib;
  • How to encapsulate the HTTP request tool class.

Part 1: A brief summary of common configurations for HTTP requests

Hello everyone, in Java development, we often encounter the need to call interface services provided by third parties. The common form is HTTP + JSON. Below, we will explain the common settings of http requests.

http provides a variety of request methods to meet our daily needs, first explain according to the request method:

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE

In RESTful API development, we can design our API interface according to these request methods. Example:

  • GET: get resources
  • POST: submit resource
  • PUT: update complete resource
  • PATCH: update some resources
  • DELETE: delete resource

parameter format

  • form form
  • json

other

  • Timeout setting

Part 2: HttpUtils using JavaLib

simple get request

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

Response result:

<!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>Baidu, you will know</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><span class="b g s_btn_wr"><input type=submit id=su value=Baidu class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> < a href=http://news.baidu.com name=tj_trnews class=mnav>News</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>map</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>video</a> < a href=http://tieba.baidu.com name=tj_trtieba class=mnav>Tieba</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login & amp ;amp;tpl=mn & amp;amp;u=http://www.baidu.com/?bdorz_come=1 name=tj_login class=lb>login</a> </noscript> <script>document.write( '<a href="http://www.baidu.com/bdorz/login.gif?login & amp;tpl=mn & amp;u=' + encodeURIComponent(window.location.href + (window. location.search === "" ? "?" : " & amp;") + "bdorz_come=1") + '" name="tj_login" class=" lb">Login</a>'); </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">More product</a> < /div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>About Baidu</a> < a href=http://ir.baidu.com>About Baidu</a></p> <p id=cp> & amp;copy;2017 & amp;nbsp;Baidu & amp;nbsp;<a href=http://www.baidu.com/duty/>Must read before using Baidu< /a> & amp;nbsp; <a href=http://jianyi.baidu.com/class=cp-feedback>Feedback</a> & amp;nbsp;Beijing ICP Certificate No. 030173& amp;nbsp;< img src=//www.baidu.com/img/gs.gif></p> </div> </div> </div> </body> </html>

Through a simple attempt, prove two points:

  • First, this is still usable;
  • Second, it turns out to be so easy to use.

If you want to write an http request by hand, maybe you have a blank in your mind, and you will search for various materials.

Let’s try another complex

interface:

Interface address: https://erwin-api.fengwenyi.com/erwin/blog/page
Request method: GET
Parameters: currentPage, pageSize
Parameter format: form
Response: application/json

String url = "https://erwin-api.fengwenyi.com/erwin/bookmark/page?currentPage=1 & amp;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);
}

Response result:

{
    "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 Chinese shows the solution to the yellow box_vscode Chinese characters are framed_YJer's Blog-CSDN Blog",
                "url":"https://blog.csdn.net/qq_33249042/article/details/123252625",
                "icon": null,
                "classifyName":"Software",
                "classifyId":"1522587269600481281"
            },
            {
                "id":"1632640455110922241",
                "timestamp": 1678086792000,
                "enabledState":null,
                "name":"Four ways of init-method and destroy-method in Spring_Starry Night Gufan's Blog-CSDN Blog",
                "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 implements navigation bar - Nuggets",
                "url":"https://juejin.cn/post/7084871748608327687",
                "icon": null,
                "classifyName":"Frontend",
                "classifyId":"1525554881275990018"
            },
            {
                "id":"1631593154401636354",
                "timestamp": 1677837096000,
                "enabledState":null,
                "name":"Spring bean creation process source code analysis - Tencent Cloud Developer Community-Tencent Cloud",
                "url":"https://cloud.tencent.com/developer/article/1631160",
                "icon": null,
                "classifyName":"Spring",
                "classifyId":"1522586360887742466"
            },
            {
                "id":"1631592987673858050",
                "timestamp": 1677837056000,
                "enabledState":null,
                "name":"SpringBoot's container startup source code analysis and Bean loading_springboot loading bean source code_minemine0418's blog-CSDN blog",
                "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 life cycle - Zhihu",
                "url":"https://zhuanlan.zhihu.com/p/158468104",
                "icon": null,
                "classifyName":"Spring",
                "classifyId":"1522586360887742466"
            },
            {
                "id":"1631579732104548354",
                "timestamp": 1677833896000,
                "enabledState":null,
                "name":"One article to understand the life cycle of Spring Bean_life cycle of spring bean_Old Weekly Chat Architecture Blog-CSDN Blog",
                "url":"https://blog.csdn.net/riemann_/article/details/118500805",
                "icon": null,
                "classifyName":"Spring",
                "classifyId":"1522586360887742466"
            },
            {
                "id":"1630768897186697218",
                "timestamp": 1677640578000,
                "enabledState":null,
                "name":"MySQL counts the number of records of multiple conditions at the same time_ztnhnr's blog-CSDN blog",
                "url":"https://blog.csdn.net/ztnhnr/article/details/107165942",
                "icon": null,
                "classifyName":"MySQL",
                "classifyId":"1522586805693681666"
            },
            {
                "id":"1630768792098410497",
                "timestamp": 1677640553000,
                "enabledState":null,
                "name":"sql query the data of the past seven days, the past two weeks, and the past month_sql data of the last week_Sincerity Zeling' Blog-CSDN Blog",
                "url":"https://blog.csdn.net/wenchangwenliu/article/details/119891790",
                "icon": null,
                "classifyName":"MySQL",
                "classifyId":"1522586805693681666"
            },
            {
                "id":"1630480535938764801",
                "timestamp": 1677571827000,
                "enabledState":null,
                "name":"Which open source process engine is better, how to choose? - Zhihu",
                "url":"https://zhuanlan.zhihu.com/p/369761832",
                "icon": null,
                "classifyName":"Java",
                "classifyId":"1522586296119300097"
            }
        ]
    }
}

The response results are still in line with expectations.

Of course, there are other things in HTTP, such as timeout, etc. Let’s take a look at the full version of the request example:

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

The response result is also the same as above.

Part III: Share how HttpUtils is encapsulated

First, Request and Response correspond to HTTP requests and responses. The package path is as follows:

  • com.fengwenyi.javalib.http.Request
  • com.fengwenyi.javalib.http.Response

In addition, com.fengwenyi.javalib.http.Request.Option is used to store HTTP parameter configuration.

The source of ideas for this part is Spring Cloud OpenFeign.

In order to be compatible with multiple HTTP tools to implement requests, HttpClientFactory is introduced. Other tool classes only need to implement the HttpClient interface.

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

Ok, let’s take a look at the code below:

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

Therefore, you only need to implement the HttpClient#execute interface.

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