0% found this document useful (0 votes)
23 views4 pages

CSE 312 Lab Report 1

This lab report details an experiment on implementing HTTP GET and POST methods using Java. The objectives include establishing an HTTP connection, sending a GET request, and printing the response. The program successfully retrieves and displays webpage content, demonstrating the functionality of HTTP GET requests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

CSE 312 Lab Report 1

This lab report details an experiment on implementing HTTP GET and POST methods using Java. The objectives include establishing an HTTP connection, sending a GET request, and printing the response. The program successfully retrieves and displays webpage content, demonstrating the functionality of HTTP GET requests.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)


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

Lab Report NO # 01
Course Title: Data Communication Lab
Course Code: CSE 312 Section: 223 D5

Lab Experiment Name: Implementing HTTP GET and POST methods.

Student Details

Name ID

1. Taher Mahmud Monmoy 223002120

Submission Date : 02/03/24


Course Teacher’s Name : Ms. Rusmita Halim Chaity

Lab Report Status


Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
TITLE OF THE LAB REPORT EXPERIMENT
Implementing HTTP GET and POST methods.

OBJECTIVES
• To establish HTTP connection using Java.
• To send GET request to specified website and print the response.

PROCEDURE
1. Initialize URL connection and create URL object with website link.
2. Open connection using HttpURLConnection.
3. Set HTTP request method to GET.
4. Send request and store response code and message.
5. Print response message and code.
6. If response code is 200, then:
i) Use InputStreamReader and BufferedReader to read response line by line.
ii) Print the response.
iii) Return the values in dotted-decimal format.
7. If response code is not 200, then print error message.
8. End the program.

IMPLEMENTATION
import java.io.*;
import java.net.*;

public class GET {


public static void main(String[] args) throws Exception {
String urlString = "https://webcode.me/";
HttpURLConnection con;

@SuppressWarnings("deprecation")
URL url = new URL(urlString);
con = (HttpURLConnection) url.openConnection();

con.setRequestMethod("GET");
con.setInstanceFollowRedirects(false);
con.setConnectTimeout(5000);
con.setReadTimeout(5000);

int responseCode = con.getResponseCode();


String responseMessage = con.getResponseMessage();

1
System.out.println("The response message and code from website is: " +
responseMessage + " " + responseCode);
System.out.println("Now we get our website:");

if (responseCode == 200) {
InputStreamReader in = new InputStreamReader(con.getInputStream());
BufferedReader read = new BufferedReader(in);
String str;

while ((str = read.readLine()) != null) {


System.out.println(str);
}
} else {
System.out.println("Error: " + responseCode + " - " +
responseMessage);
}
}
}

OUTPUT
1. https://webcode.me/:

Figure 1: Result from https://webcode.me/

2
2. https://www.youtube.com/:

Figure 2: Result from https://www.youtube.com/

ANALYSIS AND DISCUSSION


The program in this report builds HTTP connection in Java and sends a GET request to the
specified website. It retrieves the response code and message and if the response is 200, then it
prints the webpage content. If an error occurs, it displays the error message.

SUMMARY
In summary, the program successfully fetches and displays webpage content using Java's
HttpURLConnection. It demonstrates how to the HTTP GET request works in Java.

You might also like