Making HTTPS Requests in Java

To call an HTTPS interface, you can use HttpURLConnection or HttpClient in Java. Here is an example of calling an HTTPS interface using HttpURLConnection.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpsExample {
    public static void main(String[] args) throws IOException {
        // 创建URL对象
        URL url = new URL("https://api.example.com/endpoint");

        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为GET
        connection.setRequestMethod("GET");

        // 获取响应码
        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);

        // 读取响应内容
        InputStream inputStream = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        // 输出响应内容
        System.out.println("Response Body: " + response.toString());

        // 关闭连接
        connection.disconnect();
    }
}

If you want to call HTTPS interfaces using HttpClient, you can use the Apache HttpClient library. You can add the following dependency through Maven or Gradle.

Maven: expert or knowledgeable person

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>

Gradly:

implementation 'org.apache.httpcomponents:httpclient:4.5.13'

Here is an example of calling an HTTPS interface using HttpClient:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpsExample {
    public static void main(String[] args) throws IOException {
        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLContext(SSLContextBuilder.create().loadTrustMaterial(new TrustSelfSignedStrategy()).build())
                .setSSLHostnameVerifier(new NoopHostnameVerifier())
                .build();

        // 创建HttpGet请求对象
        HttpGet httpGet = new HttpGet("https://api.example.com/endpoint");

        // 发送请求,获取响应
        HttpResponse response = httpClient.execute(httpGet);

        // 获取响应内容
        HttpEntity entity = response.getEntity();
        String responseBody = EntityUtils.toString(entity);

        // 输出响应内容
        System.out.println("Response Body: " + responseBody);

        // 关闭HttpClient
        httpClient.close();
    }
}

There are two common methods for calling HTTPS interfaces, you can choose one according to your own needs to implement.

bannerAds