I am looking for a solution to programming project #12 on page 814. Write a java
ID: 3858941 • Letter: I
Question
I am looking for a solution to programming project #12 on page 814.
Write a java program that serves as a primitive web browser. For this assignment it merely needs to input a server name and displays the HTML that is sent by the web browser. A web browser nornally listens on port 80. Upon connection the server expects to be sent a string that identifies what web page to receive (use for the root) and what protocal is used. The next line is to Host and then a blank like. For example, to get teh default page on Wikipedia the Java program would connect to port 80 and send: GET / HTTP /1.1 Host: www.wikipedia.org(blank line)
The Wikipedia server would then send back the HTML for the site which your program should display in text. For a more challening program, parse and render the HTML in a human-friendly format instead of printing out the raw HTML.
Explanation / Answer
Below is the sample class to get the raw html content. import java.net.*; import java.io.*; public class GetHTML { public static void main(String args[]) throws Exception{ URL url = new URL("https://www.google.com"); URLConnection con = url.openConnection(); InputStream in = con.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(in)); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line+" "); } System.out.println("Response: " + result); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.