Web Science/Part1: Foundations of the web/Web Architecture/Hypertext Transfer Protocol/A Simple Web Client

From Wikiversity
Jump to: navigation, search

New-here.png NewDiscussionButton.png Feed-icon.svg



Quick Navigation: Video - Script - further Reading - Quiz - Discussion


Video of the lesson: A simple HTTP client

Add-to-watchlist-HOCH.png

Quick Navigation: Video - Script - further Reading - Quiz - Discussion

Script for: A simple HTTP client
package demo;
/**
 * this program is written by Rene Pickhardt and in the public domain GPLv3
 * contact: http://www.rene-pickhardt.de or rene@rene-pickhardt.de
 * 
 * The purpose is to demonstrate how to build a simple Web Client on top
 * of TCP / IP. Therefor we just make a very simple HTTP GET request. 
 */
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
 
public class SimpleWebClient {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			// create a TCP connection on port 80
			Socket httpSocket = new Socket("studywebscience.org", 80);
 
			// following HTTP/1.0 this is a simple get request
			String getRequest = "GET /test/simple.html HTTP/1.0\r\n\r\n";
 
			// send the data of the HTTP/1.0 GET request over the wire
			OutputStream requestStream = httpSocket.getOutputStream();	
			requestStream.write(getRequest.getBytes());
 
			// retrieve the response
			BufferedInputStream responseStream = new BufferedInputStream(httpSocket.getInputStream());
 
			int tmp = -1;
 
			while ((tmp = responseStream.read())>0){
				System.out.print(""+(char) tmp);
			}
			// close the connection (socket will be closed by the server!)
			requestStream.close();
			responseStream.close();
 
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
	}
 
}

Quick Navigation: Video - Script - further Reading - Quiz - Discussion

Further information, readings and exercises about A simple HTTP client


Quick Navigation: Video - Script - further Reading - Quiz - Discussion

Check your understanding of: A simple HTTP client

1. Which of the following is a correct way of creating a JAVA socket when making an HTTP GET request to http://studywebscience.org/test/simple.html?

new Socket("studywebscience.org",80);
new Socket("studywebscience.org");
new Socket("studywebscience.org/test/simple.html",80);
new Socket("http://studywebscience.org/test/simple.html",80);
new Socket("http://studywebscience.org/test/simple.html");

2. In the video I say You could get an UnknownHostException because someone might not know our server studywebscience.org. What do I actually mean with this statement?

The domain studywebscience.org could not be registered in the domain name system.
The host name studywebscience.org could not be entered in my local host file.
No route to the IP address of the resolved host can be found
the computer might not be connected to the internet:

3. When will an IOException be thrown in the program?

If the host with the resolved IP Address is not online
If network congestions takes place and packages are dropped
If the internet is censored and the packages to the host are blocked
If the DNS cannot resolve the hostname
Always if the computer is not connected to the internet
if the requested file does not exist on the Web server

Your score is 0 / 0


Quick Navigation: Video - Script - further Reading - Quiz - Discussion

Discuss A simple HTTP client with others

QuestionButton.png DiscussionButton.png

Join the discussion: Ask and questions and provide answers

  • If you click the ask a question button you're question will appear at the bottom of this list.
  • If you click the give an answer you will be able to provide an answer to the questions that have been asked so far.

Notes on the video

I wanted to mention that you should please use an external keyboard or mic. But your typing does not seem to be in sync with the deep, loud and constant background noises. Whatever it was, its sound is prevalent thanks to my subwoofer. --Onse (discusscontribs) 09:13, 12 November 2013 (UTC)

Check your understanding clarification

Q2: In the video I say You could get an UnknownHostException because someone might not know our server studywebscience.org. What do I actually mean with this statement?

a) Why the following is true?

  • The host name studywebscience.org could not be entered in my local host file.

We always will ask DNS external servers, if there is no such host in your /etc/hosts .

b) Should be true:

  • the computer might not be connected to the internet.

Q3: When will an IOException be thrown in the program?

You marked as true:

  • If the internet is censored and the packages to the host are blocked.

I think it depends on how the host is blocked. I see several options. Firstly, the host can be redirected to the special ip address, where it will be a statement that the site is closed (no error actually). Secondly, it can be blocked by ip, but I can not find information on how it works. Hence, I do not mind how Java will react, maybe it will cause "NoRouteToHostException"? --oleamm (discusscontribs) 19:41, 25 November 2013 (UTC)

QuestionButton.png DiscussionButton.png


Previous-mooc-lesson.png Parent-mooc-lesson.png Next-mooc-lesson.png

navigational context


Video
and script
Associated Lesson
Making HTTP requests
A Simple Web Client
A Simple Web Server
The HTTP Header
Content negotiation
Summary, Further readings, Homework

The following video of the flipped classroom associated with this topic are available:

You can find more information on wiki commons and also directly download this file