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

CN Ex2

CN.lab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views4 pages

CN Ex2

CN.lab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

EX.

NO:
DATE: Write a HTTP web client program to download a
web page using TCP Sockets.

AIM:
To create a program to use TCP sockets to establish a connection to a web server and
download a web page by sending an HTTP GET request.

ALGORITHM:
1. Obtain the URL of the web page you want to download and specify the output file
path where you want to save the downloaded content.
2. Create a URL object and parse the input URL to extract the scheme (HTTP or
HTTPS), host, and path.
3. Check if the URL uses HTTPS. If it does, proceed with HTTPS handling; otherwise)
inform the user that the example only supports HTTPS URLs.
4. Create an Https URLConnection object and set the request method to "GET".
Establish an HTTPS connection to the web server using openConnection(). Check the HTTP
response code to ensure a successful connection.
5. Create an output file stream to write the downloaded content to the specifie
output file path.
6. Read the content from the input stream in chunks and write it to the output file.
7. Close Streams and Connection: Close the output file stream, input stream, anD
HTTPS connection.
8. Display a message indicating whether the web page was successfully downloaded
or if there was an issue.

CODE:
import java.io.*;

import java.net.*;

import javax.net.ssl.HttpsURLConnection;

public class dhivyaHttpClient {

public static void main(String[] args) {

String urlString = "https://www.google.com";

String outputFilePath = "webpage.html";


try {

// Create a URI object to ensure the URL is well-formed

URI uri = new URI(urlString);

// Convert the URI to a URL

URL parsedUrl = uri.toURL();

// Check if the URL uses HTTPS

if ("https".equalsIgnoreCase(parsedUrl.getProtocol())) {

HttpsURLConnection httpsConnection = (HttpsURLConnection)


parsedUrl.openConnection();

httpsConnection.setRequestMethod("GET");

httpsConnection.connect();

int responseCode = httpsConnection.getResponseCode():

if (responseCode == HttpsURLConnection.HTTP_OK) {

FileOutputStream fos = new FileOutputStream(outputFilePath);

// Get the input stream to read the server's response

InputStream is = httpsConnection.getInputStream();

byte[] buffer = new byte[4096];

int bytesRead;

while ((bytesRead = is.read(buffer)) != -1) {

fos.write(buffer, 0, bytesRead);

fos.close();

is.close();

System.out.println("Web page downloaded and saved to " + outputFilePath);

} else {
System.out.println("Failed to download. HTTP response code: " + responseCode);

} else {

System.out.println("This example only supports HTTPS URLs.");

} catch (URISyntaxException e) {

System.err.println("Invalid URL syntax: " + e.getMessage());

} catch (IOException e) {

e.printStackTrace();

OUTPUT:
RESULT:
Thus the program to use TCP sockets to establish a connection to a web server and
download a web page by sending an HTTP GET request was executed successfully.

You might also like