URL and HTTP Communication
In the Java programming language, Java developers can access APIs to communicate directly with web resources using URLs and HTTP (Internet web addresses and web protocols). These web-based communication protocols allow Web API applications to connect directly to web servers, send necessary web requests, and receive responses to send requests.

Today, the main API for modern Java application design and development is the java.net.http package or library, which was introduced in Java 11. In Java 8, this was typically performed using the HttpURLConnection method.
URL Java Web Connection Method.
In Internet services, a website webpage URL (Uniform Resource Locator) indicates the digital location of all data information resources on a website on the Internet. A website’s data resources are accessed through URLs.
Example of a simple URL.
https://vcanhelpsu.com/index.html
A typical website URL might look like this.
https://vcanhelpsu.com/contact-us/
↓ ↓ ↓ ↓
Web Protocol URL Host Webpage
Creating a URL in Java Web.
In older Java 8 versions.
import java.net.*;
URL url =
new URL(“https://vcanhelpsu.com”);
Using this method, Java users can access digital information about a URL.
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPath());
Possible output.
https
vcanhelpsu.com
Common Java Used URL Methods
| # | Java URL Method | Purpose of Java URL Method |
| 1. | getProtocol() method | getProtocol() method used to return protocol value |
| 2. | getHost() method | getHost() method used to Returns host name information |
| 3. | getPort() method | getPort() java method used to return port value detail |
| 4. | getPath() method | getPath() method used to return a detail path of web resources |
| 5. | getQuery() method | getQuery() method used to return query string value |
| 6. | toString() method | toString() method used to return complete webpage URL |
HTTP Protocol Communication in Java Internet.
HTTP (Hypertext Transfer Protocol) is an important web protocol used in the internet world to exchange digital data, information, and other web resources or establish communication between a client and a web server.
Basic HTTP Protocol Communication Java Flow.
Client Web Request
|
| HTTP Request
↓
Dedicated Host or Web Server
|
| HTTP Response to Client
↓
Client Receive Request
HTTP Protocol Example.
GET /users/10 HTTP/1.1
To this client HTTP request, the web server can respond as follows:
HTTP/1.1 200 OK
And can return web resource data values like JSON or HTML as output.
HttpURLConnection concept in Java 8.
In Java 8, the HTTP protocol typically uses the HttpURLConnection method to access web resources.
Example of the GET Java request method.
import java.net.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
HttpURLConnection connection = null;
try {
URL url = new URL(“https://vcanhelpsu.com/”);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(“GET”);
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.setRequestProperty(“User-Agent”, “Mozilla/5.0”);
int status = connection.getResponseCode();
System.out.println(“What is Status -: ” + status);
InputStream stream;
if (status >= 200 && status < 400) {
stream = connection.getInputStream();
} else {
stream = connection.getErrorStream();
}
if (stream != null) {
try (BufferedReader reader =
new BufferedReader(
new InputStreamReader(stream)) {
string line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
} catch (Exception ex) {
System.out.println(“Display Error -: ” + ex.getMessage());
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
Its output.
HTTP Status Code -: 200
HTTP Status Message -: OK
<!DOCTYPE html>
<html>
…
</html>
Common Java HTTP methods.
In the Java programming language, the following are some popular HTTP web protocol request methods used to retrieve access to Internet web resources.
HTTP Used Java Web Request Methods
| # | Common HTTP Methods | Purpose of HTTP Methods |
| 1. | GET HTTP Methods | GET HTTP Methods used to retrieve client server web resources data or info |
| 2. | POST HTTP Methods | POST HTTP Methods used to send/create client server web resource data |
| 3. | PUT HTTP Methods | PUT HTTP Methods used to replace/update web resources data |
| 4. | PATCH HTTP Methods | PATCH HTTP Methodsused to partially update web resource data |
| 5. | DELETE HTTP Methods | DELETE HTTP Methods used to delete any unwanted web resources data |
Example of an important HTTP web request method.
connection.setRequestMethod(“GET”);
or
connection.setRequestMethod(“POST”);
Sending a POST request using the Java Web HTTP Protocol.
In online Internet communication, an HTTP POST request allows data to be sent from the client side to a dedicated web server or host.
URL url =
new URL(“https://vcanhelpsu.com//api”);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
Example of the POST Java request method.
import java.net.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) {
HttpURLConnection connection = null;
try {
URL url = new URL(“https://vcanhelpsu.com/”);
connection = (HttpURLConnection) url.openConnection();
//here we create a POST request method
connection.setRequestMethod(“POST”);
connection.setDoOutput(true);
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
// here we define JSON header
connection.setRequestProperty(
“Content-Type”,
“application/json; charset=UTF-8”
);
connection.setRequestProperty(
“Accept”,
“application/json”
);
// here we read data in JSON data format
String data = “{\”emp_name\”-:\”Siddhi\”}”;
// here we send POST data to connection
try (OutputStream output = connection.getOutputStream()) {
output.write(data.getBytes(StandardCharsets.UTF_8));
}
//here we get response status
int status = connection.getResponseCode();
System.out.println(“HTTP Status is -: ” + status);
System.out.println(
“Response Message is -: ” + connection.getResponseMessage()
);
// here we Read server response
InputStream stream;
if (status >= 200 && status < 400) {
stream = connection.getInputStream();
} else {
stream = connection.getErrorStream();
}
if (stream != null) {
try (BufferedReader reader =
new BufferedReader(
new InputStreamReader(
stream,StandardCharsets.UTF_8
))) {
string line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
} catch (Exception ex) {
System.out.println(
“Display Error -: ” + ex.getMessage()
);
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
Its output.
json
{
“emp_name”-: “Siddhi”
}
This setting is required in the posted example.
connection.setDoOutput(true);
This allows the web application to send data in the https request body.
HTTP status code explanation in Java web URL.
Each time an online or offline client-server receives a web-based or specific port-based communication request, a status code is displayed in the output, indicating the result of the web request sent to the client-side web browser.
Commonly used Java HTTP request status codes.
- 200 → OK Request
- 201 → Created Request
- 400 → Bad Request
- 401 → Unauthorized Request
- 403 → Forbidden Request
- 404 → Not Found Request
- 500 → Internal Server Error Issues
Java users can find this in their program terminal web browser as follows.
int status =
connection.getResponseCode();
HTTP header request in Java web concept.
Web headers in Java internet web requests provide additional digital information about the HTTP request or response.
Example of an HTTP header request.
connection.setRequestProperty(
“Accept”,
“application/json”
);
Another HTTP header web request example.
connection.setRequestProperty(
“User-Agent”,
“TestJavaApplication”
);
HttpClient request method in Java 11.
Java 11 introduced the modern HTTP client API web request method for java.net.http.
This is generally a simpler and clearer request method than HttpURLConnection.
A simple GET request method example in Java 11.
import java.net.URI;
import java.net.http.*;
public class Main {
public static void main(String[] args) {
try {
// Here we create an HTTP Client request
HttpClient client = HttpClient.newHttpClient();
// Here we create an HTTP GET request method
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(“https://vcanhelpsu.com/”))
.GET()
.build();
// here we send HTTP Request
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
// here it displays HTTP Status Code
System.out.println(
“HTTP Status Code is -: ” + response.statusCode()
);
// here it displays HTTP Response Body
System.out.println(“Website Response is -:”);
System.out.println(response.body());
} catch (Exception ex) {
System.out.println(
“Display Web Request Error -: ” + ex.getMessage()
);
}
}
}
Its output.
HTTP Status Code -: 200
Website Response -:
<!DOCTYPE html>
<html>
<head>
<title>…</title>
…
</html>
A simple GET request flow method explanation.
HttpClient Request
↓
HttpRequest Method
↓
HTTP GET Method
↓
Dedicated Web Server
↓
HttpResponse Code
↓
statusCode() + body()
GET request example.
response.statusCode();
response.body();
Use this method to retrieve the Java user response status and your content.
Java’s modern HTTP’s URL vs. URI concept.
Modern versions of the Java programming language today use HTTP APIs and URIs as request methods.
URI uri =
URI.create(“https://vcanhelpsu.com//users”);
A functional difference between URLs and URIs.
- URI → This protocol identifies a webpage resource in the online World Wide Web system.
- URL → A URL identifies a website address resource online and provides a method to access it in a client-side web browser.
For modern Java HTTP web communication, the URI method is commonly used with HttpRequest.
Java 8 Version Httpurlconnection Vs Java 11 Version Httpclient Http Request
| # | Java 8 HttpURLConnection Method | Java 11 HttpClient Method |
| 1. | Above method commonly used in java 8 version | Above method introduced in modern java 11 version |
| 2. | HttpURLConnection Method is a older API method | Java 11 HttpClient Method is Modern now day java 11 API method |
| 3. | HttpURLConnection Method More verbose style | Java 11 HttpClient Method cleaner API method concept |
| 4. | By default, it works with web uses URL method | By default, it works with web uses URI method |
| 5. | HttpURLConnection support HTTP/HTTPS communication protocol | Java 11 HttpClient Method support HTTP/1.1 and HTTP/2 protocol version |
| 6. | HttpURLConnection is a traditional approach | Java 11 HttpClient Method recommended for now day Java code |
Simple URL and HTTP Communication Flow.
URL/URI Web Methods
↓
HTTP Web Request
↓
Dedicated Web Server
↓
HTTP Response
↓
Status Code + Header + Body Output
This will provide Java developers with improved responses and allow Java users to upload files, images, and many other web resources to the server.
Conclusion of URL and HTTP Communication with Java.
- In a Java web request, a URL indicates the location of a webpage (website address) resource on the digital World Wide Web.
- The HTTP protocol indicates direct communication between a client and a server in a web request.
- In Java 8 versions, the HttpURLConnection method is commonly used.
- In modern Java 11 versions, new web request methods have been included: HttpClient, HttpRequest, and HttpResponse.
- Typical Java client-server communication HTTP requests mainly use GET, POST, PUT, PATCH, and DELETE request methods.
- In Java web communication, the HTTP response contains a status code, headers, and body.

