HttpUtil

Overview

HttpUtil is a tool class encapsulation for Http requests in simple scenarios. It encapsulates common operations of the HttpRequest object, ensuring that Http requests can be completed within a single method.

This module is based on the HttpUrlConnection encapsulation of JDK, fully supporting https, proxies, and file uploads.

Usage

Requesting Ordinary Pages

For the most commonly used GET and POST requests, HttpUtil encapsulates two methods:

  • HttpUtil.get
  • HttpUtil.post

These two methods are used to request ordinary pages and return the content of the page as a string. At the same time, some overloaded methods are provided to specify request parameters (supporting File objects for file uploads, but only for POST requests).

Example of GET request:

// Simplest HTTP request, can automatically determine the encoding through headers, etc., regardless of HTTP or HTTPS
String result1 = HttpUtil.get("https://www.baidu.com");

// When the page encoding cannot be identified, you can customize the encoding of the requested page
String result2 = HttpUtil.get("https://www.baidu.com", CharsetUtil.CHARSET_UTF_8);

// You can pass HTTP parameters separately, and the parameters will be automatically URL-encoded and concatenated in the URL
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("city", "Beijing");

String result3 = HttpUtil.get("https://www.baidu.com", paramMap);

Example of POST request:

HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("city", "Beijing");

String result = HttpUtil.post("https://www.baidu.com", paramMap);

File Upload

HashMap<String, Object> paramMap = new HashMap<>();
// For file uploads, you only need to specify the key (default is "file") in the parameters and set the value to the file object. For users, file uploads are no different from ordinary form submissions.
paramMap.put("file", FileUtil.file("D:\\face.jpg"));

String result = HttpUtil.post("https://www.baidu.com", paramMap);

Downloading Files

Due to the Hutool-http mechanism issue, the returned result of the requested page is parsed into a byte[] all at once. If the returned result of the request URL is too large (such as file download), the memory will explode. Therefore, HttpUtil provides a separate encapsulation for file downloads. File downloads use a streaming approach for reading and writing when facing large files, with only a certain amount of cache retained in memory, and then written to the hard disk in blocks. Therefore, large files will not put pressure on memory.

String fileUrl = "http://mirrors.sohu.com/centos/8.4.2105/isos/x86_64/CentOS-8.4.2105-x86_64-dvd1.iso";

// Save the downloaded file in the E drive, and the return result is the size of the downloaded file
long size = HttpUtil.downloadFile(fileUrl, FileUtil.file("e:/"));
System.out.println("Download size: " + size);

Of course, if we want to perceive the download progress, we can use another overloaded method to call back and perceive the download progress:

// File download with progress display
HttpUtil.downloadFile(fileUrl, FileUtil.file("e:/"), new StreamProgress(){

    @Override
    public void start() {
        Console.log("Download started...");
    }
    
    @Override
    public void progress(long progressSize) {
        Console.log("Downloaded: {}", FileUtil.readableFileSize(progressSize));
    }
    
    @Override
    public void finish() {
        Console.log("Download completed!");
    }
});

After implementing the StreamProgress interface, we can perceive various stages during the download process.

Of course, the tool class provides a more abstract method: HttpUtil.download. This method requests a URL and writes the returned content to the specified OutputStream. Using this method, we can more flexibly convert and write out HTTP content to adapt to more scenarios.

More useful tool methods

  • HttpUtil.encodeParams encodes URL parameters, encoding only the keys and values. The provided value can be URL parameters, but cannot be just a URL.
  • HttpUtil.toParams and HttpUtil.decodeParams are two methods to convert Map parameters to URL parameter strings and convert URL parameter strings to Map objects.
  • HttpUtil.urlWithForm concatenates a URL string and Map parameters into a complete string for use in GET requests.
  • HttpUtil.getMimeType quickly retrieves the MimeType based on the file extension (the parameter can also be a complete file path).

More request parameters

If you want to set header information, timeouts, proxies, etc., please refer to the next chapter “Http Client - HttpRequest”.