Question: Complete the Java command line application. The application accepts a URL from the command line. This application should then make a HTTP request to GET

Complete the Java command line application. The application accepts a URL from the command line. This application should then make a HTTP request to GET the HTML page for that URL, then print the HTTP header as well as the HTML for the page to the console. You must use the Java socket class to do all network I/O with the webserver. Yes, Im aware this is on Stack Overflow, but you must understand how this works, as you will be tested on it. For extra credit, try to get things working with HTTPS.

YOU DONT HAVE TO SOLVE THIS PROBLEM, CAN YOU JUST PLEASE EXPLIAN THROUGHLY WHAT I AM SUPPOSED TO DO. I AM STRUGGLING WITH JAVA AND KNOW VERY LITTLE. Thank You!

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

class SocketGet {

// main entry point for the application public static void main(String args[]) { try { String urlString = arg[0]; URL httpUrl = new URL(urlString);

// TODO: open socket /* Useful links: * * https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html * */ // Socket httpSocket = ???

// open up streams to write to and read from PrintWriter request = new PrintWriter(httpSocket.getOutputStream(), true); BufferedReader response = new BufferedReader(new InputStreamReader(httpSocket.getInputStream()));

// TODO: build the HTTP "GET" request, alter the httpHeader string to take a path (e.g. http://www.examplefoo.com/dir1/page.html) /* Useful links: * * https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html * https://msdn.microsoft.com/en-us/library/aa287673%28v=vs.71%29.aspx?f=255&MSPPError=-2147217396 * */ String httpHeader = "GET / HTTP/1.1 Accept: */* Accept-Language: en-us Host: localhost Connection: close ";

// send the HTTP request request.println(httpHeader);

// read the reply and print String responseStr = response.readLine(); while ((responseStr != null) && (responseStr != "")) { System.out.println(responseStr); responseStr = response.readLine(); }

// TODO: close the socket } catch (UnknownHostException e) { System.err.println("Don't know the hostname"); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection"); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!