Java可以使用以下方法获取网络请求数据:

  1. 使用Java的URL类和URLConnection类:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

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

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

            // 读取数据
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  1. 使用Java的HttpClient类:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

public class Main {
    public static void main(String[] args) {
        try {
            // 创建HttpClient对象
            HttpClient client = HttpClientBuilder.create().build();

            // 创建HttpGet对象
            HttpGet request = new HttpGet("https://example.com");

            // 发送请求
            HttpResponse response = client.execute(request);

            // 读取数据
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这些方法可以用来获取网络请求的数据。请注意,第二种方法需要添加Apache HttpClient库的依赖

Java使用获取网络请求数据