Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Hypertext Transfer Protocol (HTTP) is the foundation protocol of the World W

ID: 3583729 • Letter: T

Question


The Hypertext Transfer Protocol (HTTP) is the foundation protocol of the World Wide Web and can be used in any client/server application involving hypertext. HTTP is a protocol for transferring plaintext, hypertext, audio, images or any Internet-accessible information. HTTP is an application layer protocol and is typically used between a Web browser and a Web server. A Web server is a machine (or a software) that stores websites contents. The aim of this project is to give students more insights, from a programming perspective, on how the interaction between the Web browser (Chrome, Internet Explorer, Firefox...etc) and the Web server happens. Use your knowledge in sockets programming to write the following programs. A client program that executes a single HTTP GET to a Web server to retrieve any type of content from any website. A client program to execute a single HTTP GET to a Web server to retrieve a complete file, from any website, and store it locally. Write a client program to do serial HTTP GET (that is, a sequence of GETs) to a Web server.

Explanation / Answer

Following Java Client program for reading the content from any website whose URL name is passed as command line argument. Name of the Program is ClientGetData.java which calls the readUrl function in main method to print the contenet from website.

//ClientGetData.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;

public class ClientGetData
{

   public static void main(String[] args) {
       url=args[1]; // we assume that user passes the url is passed as command line argument
       System.out.println(" Output: " + readURL("http://"+url));
   }

   public static String readURL(String URLpassed) {
       System.out.println("Requeted URL:" + URLpassed);
       StringBuilder sb = new StringBuilder();
       URLConnection urlConn = null; //url Connection Object
        InputStreamReader in = null; // Input Stream Reader Object
       try {
           URL url = new URL(URLpassed); //instatiation of URL Object
           urlConn = url.openConnection(); // instatiation of URLConnection Object
           if (urlConn != null)
               urlConn.setReadTimeout(60 * 1000);
           if (urlConn != null && urlConn.getInputStream() != null) {
               in = new InputStreamReader(urlConn.getInputStream(),
                       Charset.defaultCharset());
               BufferedReader bufferedReader = new BufferedReader(in);
               if (bufferedReader != null) {
                   int cp;
                   while ((cp = bufferedReader.read()) != -1) {
                       sb.append((char) cp);
                   }
                   bufferedReader.close();
               }
           }
       in.close();
       } catch (Exception e) {
           throw new RuntimeException("Exception while calling URL:"+ URLpassed, e);
       }

       return sb.toString();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote