Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions