Question

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

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\r\nAccept: */*\r\nAccept-Language: en-us\r\nHost: localhost\r\nConnection: close\r\n\r\n";

// 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");
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
Complete the Java command line application. The application accepts a URL from the command line. This...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming)...

    Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException {    if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...

  • I need help with my IM (instant messaging) java program, I created the Server, Client, and...

    I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...

  • Java only. Thanks These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet,...

    Java only. Thanks These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to EFFICIENTLY accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each of them. Unless specified otherwise, sorted order refers to the natural sorted order...

  • Please complete the give code (where it says "insert code here") in Java, efficiently. Read the...

    Please complete the give code (where it says "insert code here") in Java, efficiently. Read the entire input one line at a time and then output a subsequence of the lines that appear in the same order they do in the file and that are also in non-decreasing or non-increasing sorted order. If the file contains n lines, then the length of this subsequence should be at least sqrt(n). For example, if the input contains 9 lines with the numbers...

  • In java write a simple 1-room chat server that is compatible with the given client code.

    In java write a simple 1-room chat server that is compatible with the given client code. 9 public class Client private static String addr; private static int port; private static String username; 14 private static iter> currenthriter new AtomicReference>(null); public static void main(String[] args) throws Exception [ addr -args[]; port Integer.parseInt (args[1]); username-args[21 Thread keyboardHandler new Thread(Main: handlekeyboardInput); 18 19 while (true) [ try (Socket socket -new Socket (addr, port) println(" CONNECTED!; Printwriter writer new Printwriter(socket.getoutputStreamO); writer.println(username); writer.flush); currenthriter.set(writer); BufferedReader...

  • Please answer question correctly and show the result of the working code. The actual and demo...

    Please answer question correctly and show the result of the working code. The actual and demo code is provided .must take command line arguments of text files for ex : input.txt output.txt from a filetext(any kind of input for the text file should work as it is for testing the question) This assignment is about using the Java Collections Framework to accomplish some basic text-processing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map,...

  • Please answer the question correctly and USE THE MOST EFFICIENT TECHNIQUE OF JAVA COLLECTION FRAME WORK...

    Please answer the question correctly and USE THE MOST EFFICIENT TECHNIQUE OF JAVA COLLECTION FRAME WORK to answer it. It is very essential that you do. Please make sure it is very efficient and doesnt run out of memory. A demo code required for the question is given below. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to...

  • How to solve this problem by using reverse String and Binary search? Read the input one...

    How to solve this problem by using reverse String and Binary search? Read the input one line at a time and output the current line if and only if it is not a suffix of some previous line. For example, if the some line is "0xdeadbeef" and some subsequent line is "beef", then the subsequent line should not be output. import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet;...

  • The DictionaryClient program featured in the class lecture also used the try-catch when creating a socket....

    The DictionaryClient program featured in the class lecture also used the try-catch when creating a socket. Socket are auto-closeable and thus can use a try-with-resources instead. Edit the dictionary program to use try-with-resources instead. package MySockets; import java.net.*; import java.io.*; public class DictionaryClient {       private static final String SERVER = "dict.org";    private static final int PORT = 2628;    private static final int TIMEOUT = 15000;    public static void main(String[] args) {        Socket   ...

  • Really need help with this. This is for my Advanced Java Programming Class. If anyone is...

    Really need help with this. This is for my Advanced Java Programming Class. If anyone is an expert in Java I would very much appreciate the help. I will provide the code I was told to open. Exercise 13-3   Use JPA to work with the library checkout system In this exercise, you'll convert the application from the previous exercise to use JPA instead of JDBC to access the database. Review the project Start NetBeans and open the project named ch13_ex3_library that's...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT