如何: import java.net.Authenticator; import java.net.PasswordAuthentication; import java.net.URL; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; public class BasicAuthExample { public static void main(String[] args) { try { URL url = new URL("http://example.com/api/data"); Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username", "password".toCharArray()); } }); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int status = connection.getResponseCode(); System.out.println("Response Code: " + status); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder content