温馨提示×

java如何调用https的接口

小亿
631
2024-02-05 14:02:59
栏目: 编程语言

要调用HTTPS接口,可以使用Java中的HttpURLConnection或HttpClient。 下面是使用HttpURLConnection调用HTTPS接口的示例:

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

如果你想要使用HttpClient调用HTTPS接口,可以使用Apache HttpClient库。你可以通过Maven或Gradle添加以下依赖项:

Maven:

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

Gradle:

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

下面是使用HttpClient调用HTTPS接口的示例:

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

这是两种常用的调用HTTPS接口的方法,你可以根据自己的需求选择其中一种来实现。

0