Java HttpURLConnection Class


The java.net package contains URLConnection as an abstract class which creates a communication link between a Java application and a URL. This class enables operations to read data from the URL resource and write data to it.


notepad

This class enables users to download data and upload files while interacting with HTTP APIs using its HttpURLConnection subclass.


Syntax

Import Statements

import java.net.HttpURLConnection;
import java.net.URL;
import java.io.*; 

Creating a Connection

URL url = new URL("https://api.example.com/data");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

Methods of HttpURLConnection

Method Description
setRequestMethod(String method) Sets HTTP method (GET, POST, etc.)
getInputStream() Reads response from server
getOutputStream() Sends data to server
setDoOutput(boolean) Enables output (for POST/PUT)
setRequestProperty(String key, String value) Adds headers (e.g., Content-Type)
getResponseCode() Returns HTTP status code
disconnect() Closes connection

Example

HTTP GET Request

 public class HttpGetExample {
    public static void main(String[] args) throws Exception {
        URL url = new URL("https://api.github.com");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");

        int status = conn.getResponseCode();
        System.out.println("Response Code: " + status);

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuilder content = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            content.append(inputLine);
        }
        in.close();
        conn.disconnect();

        System.out.println("Response:\n" + content);
    }
}

Example

HTTP POST Request

public class HttpPostExample {
    public static void main(String[] args) throws Exception {
        URL url = new URL("https://httpbin.org/post");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("POST");
        conn.setDoOutput(true); // Enable output for POST
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        String postData = "username=javauser&password=secret";

        try (OutputStream os = conn.getOutputStream()) {
            os.write(postData.getBytes());
            os.flush();
        }

        int responseCode = conn.getResponseCode();
        System.out.println("Response Code: " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

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

        System.out.println("Response:\n" + response);
    }
}
 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.