当前位置:首页 > 编程技术 > 正文

java如何访问https接口

java如何访问https接口

在Java中访问HTTPS接口,可以使用多种方式,以下是一些常见的方法: 1. 使用Java标准库`HttpsURLConnection````javaimport j...

在Java中访问HTTPS接口,可以使用多种方式,以下是一些常见的方法:

1. 使用Java标准库`HttpsURLConnection`

```java

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

public class HttpsURLConnectionExample {

public static void main(String[] args) {

try {

URL url = new URL("https://your-https-url.com");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("GET");

int responseCode = connection.getResponseCode();

System.out.println("Response Code: " + responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String inputLine;

StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {

response.append(inputLine);

最新文章