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

Complete the Java command line application. The application accepts a URL from t

ID: 3872813 • Letter: C

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” 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, I’m 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");
}
}
}

Explanation / Answer

Here's what you have to do:

Firstly, you gotta get the url from the user from command line. Then you need to make a connection to that server using Socket. Then you gotta ask for the resource which you want (Ex: GET /index.html HTTP/1.1), there GET is the request method, /index.html is the resource and HTTP/1.1 is the protocol and an important thing to notes is when you HTTP/1.1, you need to add an additional line like: Host: www.google.com. The way to make this request is to use the output stream of the socket connection and send it to the stream. Then get the input stream of the socket using BufferedReader class or something like that read the reponse in and just display the page along with the headers.

Let me know if you have any doubts.

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