Back to blog

How to Send HTTP Requests in Java

How to send HTTP requests in Java
author avatar

Yelyzaveta Hayrapetyan

2025-07-15

5 min read

In any programming language, sending HTTP requests over the web is a basic, although powerful task. It’s the building block of the internet and the main tool for a device to communicate with another device over the web. In software, HTTP requests are used in various ways, such as API calls, web scraping, or simple communication between a website and a web server. Let’s look at how to perform HTTP requests in the Java programming language.

Preparing the environment

To start off, you should have the Java development kit installed on your computer.

You can easily do that by simply downloading and installing the official installer from the Java downloads website here

After that’s installed, you can either install an IDE for Java, or use a simple code editor like VS Code and run the code from the terminal. We’ll be doing the latter for this tutorial.

Try creating a file named Main.java in a folder of your choice and open up a terminal for that folder.

Next, you can open the newly created Main.java file in your preferred code editor and enter this code:

class Main {	
public static void main(String[] args){      
		System.out.println("Hello world!");
}
}

This is our initial Java program, which we will be building upon. You can run it by running these commands in your terminal:

javac Main.java
java Main

Your terminal should now say Hello world! , indicating that your initial Java program is set up.

Choosing an HTTP client in Java

With your initial Java program set up, we can start constructing an HTTP request. 

To perform an HTTP request in Java, you would normally use some HTTP client class provided in the Java standard development kit. You shouldn’t need to install anything in addition, as Java provides a few different options for that. 

The original way in older Java versions was to use a class called java.net.HttpURLConnection. However, it does require more code to use properly, and is regarded as outdated.

The modern way would be to use either the built-in client, available from Java 11+, called java.net.http.HttpClient, or a third party client provided from Apache, or OkHttp. These  third party clients are oftentimes used more widely in production environments, however for this tutorial, we’ll be using the native Java HttpClient

Let’s delete our Hello World code, define a URL we want to use and create a new HTTP client.

We also need to import the HTTP client from Java itself.

Here’s what it should look like:

import java.net.http.HttpClient;

class Main {	
public static void main(String[] args){      
		String url = "https://ip.oxylabs.io/";
		HttpClient client = HttpClient.newHttpClient();
}
}

We’ll be using this Oxylabs URL for checking your own IP address as an example. 

Sending a GET request

Now that we have our client defined, we can continue building our HTTP request. 

Let’s start with a GET request, that we will use to see your current IP address. 

We’ll need to import some additional dependencies in order to build the GET request.

This includes a URI dependency, used to construct the destination of the request from a string, as well as the request and response objects.

Here’s what it should look like:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

class Main {
public static void main(String[] args){      
		String url = "https://ip.oxylabs.io/";
		HttpClient client = HttpClient.newHttpClient();
		HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .GET()
                .build();

}
}

After we’ve built the request, all that’s left is to execute it. We should expect an HttpResponse object as a return value that contains a string. 

It’s also a good practice to wrap the execution part in a try/catch block, to capture any errors that might occur when sending the request. 

Here’s the full request:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.IOException;

class Main {
public static void main(String[] args){      
		String url = "https://ip.oxylabs.io/";
		HttpClient client = HttpClient.newHttpClient();
		HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .GET()
                .build();
		try {
            	HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            	System.out.println("Status code: " + response.statusCode());
            	System.out.println("Response body: " + response.body());
        	} 
catch (IOException | InterruptedException e) {
            		e.printStackTrace();
        	}

}
}

If you run it with the commands mentioned before, you should see a 200 status code and your own IP address in your terminal, which should look like this:

javac Main.java
java Main

Status code: 200
Response body: <your_ip_address>

Sending a POST request

In the previous section, we started off by sending a GET request to ip.oxylabs.io.GET requests are used specifically for retrieving data from a website, or API. 

However, if we wish to send some data from our end to an API, we should send a POST request, that contains some body in some well known format, like, for example, JSON.

Let’s see what that can look like in Java. Because of how the language functions, you have to build a string out of some JSON object body yourself. This would usually be done by a third party library like Jackson, but for the simplicity of this tutorial, we’ll be using a small method for doing that ourselves.

Let’s write a method called mapToJson.

private static String mapToJson(Map<String, String> data) {
        StringBuilder jsonBuilder = new StringBuilder();
        jsonBuilder.append("{");
        boolean first = true;
        for (Map.Entry<String, String> entry : data.entrySet()) {
            if (!first) {
                jsonBuilder.append(",");
            }
            jsonBuilder.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue()).append("\"");
            first = false;
        }
        jsonBuilder.append("}");
        return jsonBuilder.toString();
    }

This method should convert any Map type to a JSON object, which is a standard format of sending data through a POST request.

With that done, let’s move on to building our body. First off, let’s import the Map and HashMap types from the main Java SDK. After that’s done, we can define our example body. 

We should also change up the URL to a page that accepts HTTP requests meant for testing, like httpbin.org.

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public static void main(String[] args){        

        String url = "https://httpbin.org/post";

        Map<String, String> data = new HashMap<>();
        data.put("username", "testuser");
        data.put("email", "test@example.com");


        String jsonBody = mapToJson(data);
}

Now that we have our body, we can use the code for defining and building the request from before, just with a POST request type.

Here’s what the full code should look like:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

class Main {

    private static String mapToJson(Map<String, String> data) {
        StringBuilder jsonBuilder = new StringBuilder();
        jsonBuilder.append("{");
        boolean first = true;
        for (Map.Entry<String, String> entry : data.entrySet()) {
            if (!first) {
                jsonBuilder.append(",");
            }
            jsonBuilder.append("\"").append(entry.getKey()).append("\":\"").append(entry.getValue()).append("\"");
            first = false;
        }
        jsonBuilder.append("}");
        return jsonBuilder.toString();
    }

    public static void main(String[] args){        

        String url = "https://httpbin.org/post";

        Map<String, String> data = new HashMap<>();
        data.put("username", "testuser");
        data.put("email", "test@example.com");


        String jsonBody = mapToJson(data);

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();
        
        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            System.out.println("Status code: " + response.statusCode());
            System.out.println("Response body: " + response.body());
        } 
        catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

    }
}

By running the code you should be able to see something like this in your terminal:

Status code: 200
Response body: {
  "args": {}, 
  "data": "{\"email\":\"test@example.com\",\"username\":\"testuser\"}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Content-Length": "50", 
    "Host": "httpbin.org", 
    "User-Agent": "Java-http-client/24.0.1"
  }, 
  "json": {
    "email": "test@example.com", 
    "username": "testuser"
  }, 
  "origin": "<your_ip_address>", 
  "url": "https://httpbin.org/post"
}

Adding custom headers

Now that we know how to send basic HTTP requests, let's look at how to add additional headers together with them. Headers come with almost any HTTP request you could think of. They’re important, since they can provide authentication credentials, describing what resource the client is expecting, caching and many other things. 

Let’s write an example POST request, that adds simple Content-Type headers, that signal what kind of content the request contains in a body.

To do that, we can simply copy over the code from before, and add this line when building the request:

.header("Content-Type", "application/json")

You can change the values with any header key-value pair you need. 

Here’s what the main request method looks like with headers attached to the request:

public static void main(String[] args){        

        String url = "https://httpbin.org/post";

        Map<String, String> data = new HashMap<>();
        data.put("username", "testuser");
        data.put("email", "test@example.com");


        String jsonBody = mapToJson(data);

        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();
        
        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            System.out.println("Status code: " + response.statusCode());
            System.out.println("Response body: " + response.body());
        } 
        catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

    }

Using proxies

If you need to mask your IP address while performing an HTTP request, you’ll need to use a paid proxy server

A proxy is a remote server that the request is routed through before getting to its original destination. Proxies can be of different types, such as datacenter proxies, ISP proxies, mobile proxies, residential, or even free proxies suitable for small tasks and testing. Proxies’ common use case is performing web scraping tasks, to get publicly available data from any website on the web, while concealing your own private IP. 

Let’s use Oxylabs’ Residential Proxies in this example. Residential proxies from best proxy providers allow you to use real IP addresses from various locations around the world, provided by real ISPs. You can get access to these by either retrieving the required credentials, or whitelisting your local IP address from your Oxylabs dashboard. For this example, we’ll be using a whitelisted approach.

After that’s done, we can start building a request that would be sent through the residential proxy. 

First of all, let’s add a few new imports, which will allow us to add a proxy server to our HTTP request.

import java.net.InetSocketAddress;
import java.net.ProxySelector;

Next, let’s add a few values after our JSON body declaration. This will include an object for defining the actual proxy server address, and a proxy selector object for the request itself.

It should look like this:

InetSocketAddress proxyAddress = new InetSocketAddress("pr.oxylabs.io", 10000);
ProxySelector proxySelector = ProxySelector.of(proxyAddress);

To add the proxy to your HTTP client, it’s enough to build the client like we do the request and add a proxy() call to it. 

Here’s what it looks like:

HttpClient client = HttpClient.newBuilder()
.proxy(proxySelector)
.build();

If you’ve whitelisted your IP address in your Oxylabs dashboard, after executing the code, you should see the IP address of the proxy server set as the origin. This shows that the request successfully passed through the remote proxy. 

Conclusion

We covered the basics of sending HTTP requests in Java, including how to handle the response code. From simple GET and POST requests, to adding additional information to them with headers, or even a proxy server for a different IP address. This should give a foundation for understanding how to send or request things to remote servers over the internet, with some deeper knowledge in addition to that.

Interested in more technical content? We have a bunch of topics covered on our blog, here are a few worth checking out: JavaScript Web Scraping Libraries, JavaScript vs Python, Web Scraping With Java, JavaScript cURL, cURL Converter Java.

Forget about complex web scraping processes

Choose Oxylabs' advanced web intelligence collection solutions to gather real-time public data hassle-free.

About the author

author avatar

Yelyzaveta Hayrapetyan

Senior Technical Copywriter

Yelyzaveta Hayrapetyan is a Senior Technical Copywriter at Oxylabs. After working as a writer in fashion, e-commerce, and media, she decided to switch her career path and immerse in the fascinating world of tech. And believe it or not, she absolutely loves it! On weekends, you’ll probably find Yelyzaveta enjoying a cup of matcha at a cozy coffee shop, scrolling through social media, or binge-watching investigative TV series.

All information on Oxylabs Blog is provided on an "as is" basis and for informational purposes only. We make no representation and disclaim all liability with respect to your use of any information contained on Oxylabs Blog or any third-party websites that may be linked therein. Before engaging in scraping activities of any kind you should consult your legal advisors and carefully read the particular website's terms of service or receive a scraping license.

Related articles

Web Scraper API Quick Start Guide
Web Scraper API Quick Start Guide
author avatar

Augustas Pelakauskas

2025-07-09

How to Scrape NASDAQ Data Using Python
Akvilė Lūžaitė avatar

Akvilė Lūžaitė

2025-06-27

How to Send POST Requests With cURL
How to Send POST Requests With cURL
author avatar

Yelyzaveta Hayrapetyan

2025-06-27

Get the latest news from data gathering world

I’m interested

Forget about complex web scraping processes

Choose Oxylabs' advanced web intelligence collection solutions to gather real-time public data hassle-free.