Java URLConnection Class


URLConnection is an abstract class in the java.net package that represents a communication link between a Java application and a URL. It allows both reading from and writing to a resource referenced by a URL.


notepad

It's useful for downloading data, uploading files, or interacting with HTTP APIs (via HttpURLConnection subclass).


Syntax

 URL url = new URL("https://www.example.com");
URLConnection connection = url.openConnection();
connection.connect();

Method Description
connect() Opens a communication link
getInputStream() Reads from the URL
getOutputStream() Writes to the URL
setDoOutput(true) Enables writing (POST or PUT)
getContentType() Returns MIME type
getContentLength() Returns content size
getHeaderField(String name) Retrieves a header field
setRequestProperty(String key, String value) Adds custom request headers

Example

Reading Content from a URL

import java.io.*;
import java.net.*;

public class URLConnectionExample {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://www.example.com");
            URLConnection conn = url.openConnection();

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Example

Setting Request Properties

URL url = new URL("https://www.example.com");
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
 

Example

POST Request with HttpURLConnection

import java.io.*;
import java.net.*;

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

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

        String data = "username=javauser&password=1234";

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

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String responseLine;
        while ((responseLine = reader.readLine()) != null) {
            System.out.println(responseLine);
        }
        reader.close();
    }
}
 




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.