CSE 312 Lab Report 1
CSE 312 Lab Report 1
Lab Report NO # 01
Course Title: Data Communication Lab
Course Code: CSE 312 Section: 223 D5
Student Details
Name ID
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.*;
@SuppressWarnings("deprecation")
URL url = new URL(urlString);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setInstanceFollowRedirects(false);
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
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;
OUTPUT
1. https://webcode.me/:
2
2. https://www.youtube.com/:
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.