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 optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.