Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // There are several ways to make an HTTP request in Java. One way is to use the java.net.URL and java.net.HttpURLConnection classes.
- // Here's an example of how to send an HTTP GET request using these classes:
- import java.io.*;
- import java.net.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- URL url = new URL("http://www.example.com");
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- con.setRequestMethod("GET");
- int responseCode = con.getResponseCode();
- System.out.println("Response code: " + responseCode);
- BufferedReader in = new BufferedReader(
- new InputStreamReader(con.getInputStream()));
- String inputLine;
- StringBuffer content = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- content.append(inputLine);
- }
- in.close();
- System.out.println(content.toString());
- }
- }
- // This code sends an HTTP GET request to the specified URL and prints the response code and the contents of the response to the console.
- // Alternatively, you can use a third-party library such as Apache HttpComponents, OkHttp, or Spring's RestTemplate to make HTTP requests in Java.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement