Various ways to send http requests in java

Native java sends http request

package com.cyz;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * @author cyz
 * @since 2023/11/8 9:50
 */
public class CustHttp {
    public static String post(String targetUrl, String params) throws IOException {
        URL url = new URL(targetUrl);
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            URLConnection urlConnection = url.openConnection();
            urlConnection.setRequestProperty("Request-Origion", "SwaggerBootstrapUi");
            urlConnection.setRequestProperty("accept", "*/*");
            urlConnection.setRequestProperty("Content-Type", "application/json");

            //Post request needs to use the following two
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);

            out = new PrintWriter(urlConnection.getOutputStream());
            out.write(params);
            out.flush();
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String res = "";
            String line = "";
            while ((line = in.readLine()) != null) {
                res + = line;
            }
            return res;
        } finally {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        }
    }

    public static String get(String targetUrl, String token) throws IOException {
        URL url = new URL(targetUrl);
        BufferedReader in = null;
        try {
            URLConnection connection = url.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.setRequestProperty("Authorization", token);
            // Establish the actual connection
            connection.connect();
            // Get all response header fields
            Map<String, List<String>> map = connection.getHeaderFields();
            // Iterate through all response header fields
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            //Define the BufferedReader input stream to read the response of the URL
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

            String res = "";
            String line = "";
            while ((line = in.readLine()) != null) {
                res + = line;
            }
            return res;
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        String post = post("http://10.21.1.154:19500/api/v1/user/login", "{\
" +
                "\t"captcha": "1",\
" +
                "\t"code": "1",\
" +
                "\t"password": "123456",\
" +
                "\t"username": "superAdmin"\
" +
                "}");
        System.out.println(post);

        String s = get("http://10.21.1.154:19500/api/v1/user/getBaseInfo?opId=1808",
                "/bv1CXBJczpJm6d46e + KkT2qNC00Bd9L/b9Y4Awq5P3dEaQySoJP1uCOG3uP9ubydsBSCeD/UsUoz" +
                        "7ZHTOMeNCQtEuEYeu82NV4QahqWJ8zcDgPpC5ev + wII7ApYK/z54mrfvKW2/A4ferRB4EJXuwfzoRfm3RRh" +
                        "THTiMnRuLY1BqUZP + 3rCpcinWpeCpZVGEPnImU4ZDmUNtOG4pn0Dexk3UMp838a + sgkXcRJKLCcvQd0i2Jez7MiJ" +
                        "iQrGedZVN6XXiPViETRV7NQNhBQCqZO4C9P + c9/tBuY2Qq6WrZhNhxJvphYiaIuG + zyjL + ZkYQgeKeVo6waecCnnLoBZV2" +
                        "nPmbbAmiqCrmNfueJG3wv/lBvVyKg + C1n1rjKnTIxkmVbC8v44TwBY3gi6HaBf4IFxf/hoiBgv1WqJZfPf4GpL6f2e0nhN" +
                        "s4ho5M6jQTmmjAKOei8P3FtaIogvsc9HPg==");
        System.out.println(s);
    }
}

Use Hutool tool class

rely

 <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.18</version>
        </dependency>

HttpRequest

 HttpResponse getBaidu = HttpRequest.get("www.baidu.com")
                .execute();
        System.out.println(getBaidu.body());

        HttpResponse execute = HttpRequest.post("http://10.21.1.154:19500/api/v1/user/login")
                .header("Request-Origion", "SwaggerBootstrapUi")
                .header("accept", "*/*")
                .header("Content-Type", "application/json")
                .body("{\
" +
                        "\t"captcha": "1",\
" +
                        "\t"code": "1",\
" +
                        "\t"password": "123456",\
" +
                        "\t"username": "superAdmin"\
" +
                        "}").execute();
        System.out.println(execute.body());

HttpUtil

 String s = HttpUtil.get("www.baidu.com");
        System.out.println(s);

HttpResponse execute = HttpUtil.createPost("http://10.21.1.154:19500/api/v1/user/login")
                .header("Request-Origion", "SwaggerBootstrapUi")
                .header("accept", "*/*")
                .header("Content-Type", "application/json")
                .body("{\
" +
                        "\t"captcha": "1",\
" +
                        "\t"code": "1",\
" +
                        "\t"password": "123456",\
" +
                        "\t"username": "superAdmin"\
" +
                        "}").execute();
        System.out.println(execute.body());

resttemplate

(omitted)