Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Green University of Bangladesh 

Department of Computer Science and Engineering (CSE)


Faculty of Sciences and Engineering
Semester: (Spring, Year:2023), B.Sc. in CSE (Day)

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]

Lab Report Status


Marks: …………………………………                              Signature: .....................
Comments: ..............................................                              Date: ..............................

1. TITLE OF THE LAB REPORT EXPERIMENT


GET method implementation of HTTP using JAVA code.

2. OBJECTIVES

The main objectives are-


1. To demonstrate how to implement a GET request using Java to send an
HTTP request to a web server and receive a response.
2. To provide an example of how to use the HttpURLConnection class to
establish a connection to a web server and set the request method.
3. To show how to use the BufferedReader class to read the response from the
server.
4. To print the JSON response received from the server to the console.
5. To serve as a starting point for more complex Java programs that need to
interact with web APIs or other web services.

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

The step-by-step procedure of this code are written below:


1. Import the necessary Java classes: java.io.BufferedReader,
java.io.InputStreamReader, java.net.HttpURLConnection, and java.net.URL.
2. Define the HttpGetRequest class, which contains the main method for the
program.
3. Initialize a String variable url with the URL to send the GET request to.
4. Create a HttpURLConnection object con to establish a connection to the
specified URL using the openConnection() method.
5. Set the request method for the con object to "GET" using the
setRequestMethod() method.
6. Create a BufferedReader object in to read the response from the server.
7. Read the response from the server using the readLine() method of in, and
append each line to a StringBuffer object response.
8. Close the in object using the close() method.
9. Print the JSON response received from the server to the console using
System.out.println().
10.If any errors occur during the GET request, catch them in a catch block and
print the error message to the console.

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";

// Create a HttpURLConnection object to open a connection to the URL


HttpURLConnection con = (HttpURLConnection) new
URL(url).openConnection();

// Set request method to GET


con.setRequestMethod("GET");

// Read the response from the server


BufferedReader in = new BufferedReader(new
InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

// Print the response


System.out.println(response.toString());
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

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.

6. ANALYSIS AND DISCUSSION

1. The code imports necessary Java classes: java.io.BufferedReader,


java.io.InputStreamReader, java.net.HttpURLConnection, and java.net.URL
2. The HttpGetRequest class is defined, which contains the main method for
the program.
3. In the main method, a String variable url is initialized with the URL to send
the GET request to.
4. A HttpURLConnection object con is created to establish a connection to the
specified URL using the openConnection() method.
5. The request method for the con object is set to "GET" using the
setRequestMethod() method.
6. A BufferedReader object in is created to read the response from the server.
7. The response is read from the server using the readLine() method of in, and
each line is appended to a StringBuffer object response.
8. The in object is closed using the close() method.\
9. The JSON response received from the server is printed to the console using
System.out.println().
10. If any errors occur during the GET request, they are caught in a catch block
and the error message is printed to the console.

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.

You might also like