Professional Documents
Culture Documents
Shourav Podder Computer Networking Lab
Shourav Podder Computer Networking Lab
Lab Report No 01
Course Title: Computer Networking Lab
Course Code: CSE312 Section: 203D3
Student Details
Name ID
Shourav Podder 202002048
Assignment Date : 07 March 2023
Submission Date : 14 March 2023
Course Teacher’s Name : Mohammad Ehsan Shahmi Chowdhury
[For Teachers use only: Don’t Write Anything inside this box]
2. OBJECTIVES
By achieving these objectives, the code can help developers to learn and
understand the basics of making HTTP requests using Java, which can be applied
to various web-related applications.
3. PROCEDURE
This procedure demonstrates how to send a GET request to a web server using
Java, retrieve the response from the server, and print the response to the console.
4. IMPLEMENTATION
To implement the GET method using Java code, we need to create a connection to
the server and send an HTTP GET request to the server. We can use the
HttpURLConnection class provided by Java to establish a connection to the server
and send the HTTP request. The following is an example code for implementing
the GET method using Java-
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
class HttpGetRequest {
public static void main(String[] args) {
try {
// URL to send GET request to
String url = "https://jsonplaceholder.typicode.com/posts/1";
5. RESULT
Output:
The result of the program will be the JSON response received from the server. In
the example code I provided, the result would be the JSON string:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio
reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\
nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem
eveniet architecto"
}
This is the response from the URL https://jsonplaceholder.typicode.com/posts/1,
which was requested using a GET method in the example code. The specific result
will depend on the URL that you are sending the GET request to, and the response
that the server sends back.
This code demonstrates how to send a GET request to a server using Java and how
to read the response from the server. The HttpURLConnection class provides a
simple way to open a connection to a URL and set the request method. The
BufferedReader class is used to read the response from the server.
7. SUMMARY:
This code provides an example of how to implement a GET request using Java to
send an HTTP request to a web server and receive a response. The code uses the
HttpURLConnection class to establish a connection to the specified URL, set the
request method to GET, and retrieve the response from the server using a
BufferedReader. The resulting response is printed to the console. This code could
serve as a starting point for more complex Java programs that need to interact with
web APIs or other web services.