how to use java urlconnection and httpurlconnection

‮sptth‬://www.theitroad.com

URLConnection is an abstract class that represents a communication link between the application and a URL. HttpURLConnection is a subclass of URLConnection that provides additional methods for handling HTTP-specific features such as authentication, caching, and cookies.

Here's an example of how to use HttpURLConnection to send an HTTP GET request and retrieve the response:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrlConnectionExample {
    public static void main(String[] args) throws Exception {
        URL url = new URL("https://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 response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        System.out.println("Response body: " + response.toString());
    }
}

In this example, we create a URL object with the target URL and use openConnection() method to create an HttpURLConnection object. We set the request method to "GET" using setRequestMethod() method. After that, we send the request using getResponseCode() method, which returns the HTTP status code of the response. We then read the response body using getInputStream() method, and append the lines to a StringBuffer. Finally, we print the response body.

You can also use URLConnection to send an HTTP GET request, like this:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionExample {
    public static void main(String[] args) throws Exception {
        URL url = new URL("https://example.com");
        URLConnection con = url.openConnection();

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        System.out.println("Response body: " + response.toString());
    }
}

In this example, we create a URL object with the target URL and use openConnection() method to create a URLConnection object. We then read the response body using getInputStream() method, and append the lines to a StringBuffer. Finally, we print the response body.

Note that URLConnection and HttpURLConnection provide methods for setting request headers, handling response headers, and managing timeouts. You can also use these classes to send POST, PUT, and DELETE requests, and to upload and download files.