How to Make HTTP GET and POST Requests with Java HttpURLConnection?
🎯 What Will You Learn in This Guide?
In this guide, you'll learn how to create HTTP GET and POST requests using Java's HttpURLConnection class.
We will implement the processes of creating a request, adding a title and reading a response with practical examples.
🧠 Technical Summary
Main topic: Creating HTTP GET and POST requests with Java java.net.HttpURLConnection class.
Purpose: To communicate with external APIs in Java applications.
Steps:
- The URL object is created.
- Connection (
HttpURLConnection) is opened. - Request method and headers are set.
- The response code is checked.
- InputStream is used for GET and OutputStream is used for POST.
- The server response is read and printed.
⚙️ Required Prerequisites
To run these examples:
- Java 8+ or above must be installed.
- Test Server: A local Tomcat or HTTP/HTTPS environment similar to
https://test.ornek.com:9090/APIcan be used.
🚀 HttpURLConnection Usage Steps
To create an HTTP request in Java, follow these steps:
Create URL Object
URL url = new URL("https://test.ornek.com:9090/API");
➡️ Defines the address of the target server.
Open Link
HttpURLConnection con = (HttpURLConnection) url.openConnection();
➡️ Creates an HTTP connection.
Select Request Type
con.setRequestMethod("GET");
➡️ GET or POST type is selected.
Add Title
con.setRequestProperty("User-Agent", "Mozilla/5.0");
➡️ Request header is added.
Read Response Code
int responseCode = con.getResponseCode();
➡️ 200 = Success, other codes are error status.
Read Answer (GET)
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
➡️ Reads the data stream returning from the server.
Send Data (POST)
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write("userName=GenixNodeGelis".getBytes());
os.close();
➡️ Sends POST data to the server.
💻 Sample Code – GET and POST
package com.genixnode.net;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class GenixNodeHttpConnection {
private static final String KULLANICI_AJANI = "Mozilla/5.0";
private static final String GET_URL = "https://test.ornek.com:9090/API";
private static final String POST_URL = "https://test.ornek.com:9090/API/login";
private static final String POST_PARAMETRELER = "kullaniciAdi=GenixNodeGelis";
public static void main(String[] args) throws IOException {
sendGET();
System.out.println("--- GET İsteği Tamamlandı ---");
sendPOST();
System.out.println("--- POST İsteği Tamamlandı ---");
}
private static void sendGET() throws IOException {
URL obj = new URL(GET_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", KULLANICI_AJANI);
int responseCode = con.getResponseCode();
System.out.println("GET Yanıt Kodu :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) response.append(inputLine);
in.close();
System.out.println(response);
} else System.out.println("GET isteği başarısız.");
}
private static void sendPOST() throws IOException {
URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", KULLANICI_AJANI);
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMETRELER.getBytes());
os.flush();
os.close();
int responseCode = con.getResponseCode();
System.out.println("POST Yanıt Kodu :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) response.append(inputLine);
in.close();
System.out.println(response);
} else System.out.println("POST isteği başarısız.");
}
}
🔒 HTTPS Requests (SSL/TLS)
If communication will be established over the HTTPS protocol, javax.net.ssl.HttpsURLConnection should be used instead of HttpURLConnection. This class automatically handles SSL/TLS handshake operations.
💬 Frequently Asked Questions (FAQ)
- Which is better: HttpURLConnection or HttpClient?
HttpURLConnection is sufficient for basic projects; HttpClient (Java 11+) has modern, asynchronous support.
- How to set timeout?
You can use setConnectTimeout() and setReadTimeout() methods in milliseconds.
- Can I send data in JSON format?
Yes, add Content-Type: application/json to the header and write the JSON string.
- Are redirects (301/302) automatic?
Yes, by default HttpURLConnection follows redirects.
- Is extra configuration required for HTTPS?
No, HttpsURLConnection handles SSL operations automatically.
✅ Result
In this guide, you learned how to send HTTP GET and POST requests using Java's HttpURLConnection class. With this information, your Java applications can securely exchange data with external APIs.
🚀 Test this code in your own project — you can try your Java application on the GenixNode platform now! ☁️

