Untitled
unknown
plain_text
a year ago
1.1 kB
2
Indexable
Never
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpRequestExample { public static void main(String[] args) { try { // Create URL object URL url = new URL("http://example.com/api/data"); // Open connection HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // Set request method connection.setRequestMethod("GET"); // Read response BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); // Print response System.out.println(response.toString()); // Close connection connection.disconnect(); } catch (IOException e) { e.printStackTrace(); } } }