/* * Copyright (c) 2001 Matthew Feldt. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided the copyright notice above is * retained. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND WITHOUT ANY EXPRESSED OR * IMPLIED WARRANTIES. */ /** * HttpClient.java * * Java Examples In A Nutshell Copyright (c) 2000 David Flanagan * Exercise 5-2: * Modify the HttpClient program of Example 5-4 so that it uses a newer version * of the HTTP protocol. To do this, you have to send a somewhat more * complicated GET request to the web server. Use the HttpMirror program of * Example 5-5 to find what form this request should take. HTTP Versions 1.0 * and later add a version number to the GET request and follow the GET line * with a number of header lines followed by a blank line that serves to * terminate the request. * * For this excercise the only header you need to include is the User-Agent line, * which should identify the web client that you are using. Since you are writing * your own web client, you give it any name you like! Be sure to follow * your GET request and User-Agent header with a blank line, or the web server * will keep waiting for more headers and will never respond to your request. * Whe you get this program working, you should notice that web servers * respond differently to requests from it than they did to requests from the * original HttpClient program. When a client requests data using HTTP 1.0 or 1.1, * the server sends a version number, a status code, and a number of reponse * header lines before it sends the actual requested file. * * @author Matthew Feldt * @version 1.0, 03/02/2001 17:30 */ package com.feldt.examples.net; import java.io.*; import java.net.*; public class HttpClient { public static void get(String in, OutputStream out) throws Exception { String protocol, local, host, filename; int port; // use the URL class to parse the input URL url = new URL(in); protocol = url.getProtocol(); if (! protocol.equals("http")) // only support http throw new IllegalArgumentException("Only support HTTP protocol."); if ((host = url.getHost()) == null) throw new IllegalArgumentException("Invalid host specified."); // if no port, use the default HTTP port if ((port = url.getPort()) == -1) port = 80; // if no filename, request root directory index if((filename = url.getFile()) == "") filename = "/"; // open a network socket Socket socket = new Socket(host, port); // get input and output streams from the socket InputStream from = socket.getInputStream(); PrintWriter to = new PrintWriter(socket.getOutputStream()); // send the HTTP GET v1.0 commands to the web server to.print("GET " + filename + " HTTP/1.0\n"); /* Host header required for HTTP/1.1 if ((local = InetAddress.getLocalHost().getHostName()) == null) local = "unknown"; to.print("Host: " + local + "\n"); */ to.print("User-Agent: none\n\n"); to.flush(); // read server response and write it to the specified output stream byte[] buf = new byte[4096]; int bytes_read; while((bytes_read = from.read(buf)) != -1) out.write(buf, 0, bytes_read); // close the server and file streams socket.close(); out.close(); } static class Test { public static void main(String[] args) { final String usage = "Usage: java HttpClient []"; OutputStream out; try { // verify command line if ((args.length != 1) && (args.length != 2)) { System.err.println(usage); System.exit(-1); } // determine output stream if (args.length == 2) out = new FileOutputStream(args[1]); else out = System.out; // get the url HttpClient.get(args[0], out); } catch (Exception e) { System.err.println(e); System.err.println(usage); } } } }