Web Science/Part1: Foundations of the web/Hypertext Transfer Protocol/A simple web client

Da Wikiversità, l'apprendimento libero.

A simple web client

Obiettivi di apprendimento

nessun obbiettivo di apprendimento definito
Tu puoi definire gli obiettivi di apprendimento qui.
In generale puoi usare il bottone di modifica nell'angolo in alto a destra di una sezione per modificarne il contenuto.



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();	
			// 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!)
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block




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