/** * Copyright (c) 2002 by Phil Hanna * All rights reserved. * * You may study, use, modify, and distribute this * software for any purpose provided that this * copyright notice appears in all copies. * * This software is provided without warranty * either expressed or implied. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Mainline for running the Web client */ public class MainWebClient { /** * Reads command line parameters, creates a new * <code>WebClient</code> object, then invokes it. */ public static void main(String[] args) throws IOException { // Default values String host = "localhost"; int port = 80; // Use values from command line, if specified for (int i = 0; i < args.length; i++) { String arg = args[i]; if (arg.equals("-h") || arg.equals("-help")) { showUsage(); return; } else if (arg.equals("-host") && ++i < args.length) host = args[i]; else if (arg.equals("-port") && ++i < args.length) port = Integer.parseInt(args[i]); } // Create and start the Web client new WebClient(host, port).run(); } /** * Displays the calling syntax */ private static void showUsage() { String[] text = { "usage: java com.jspcr.debug.webclient.Main" + " [-host ]" + " [-port ]", }; for (int i = 0; i < text.length; i++) System.out.println(text[i]); } } /** * Copyright (c) 2002 by Phil Hanna * All rights reserved. * * You may study, use, modify, and distribute this * software for any purpose provided that this * copyright notice appears in all copies. * * This software is provided without warranty * either expressed or implied. */ /** * A command-line interface to a Web server */ class WebClient { // Instance variables private String host; private int port; /** * Creates a new Web client * @param host the HTTP server * @param port the server port number */ public WebClient(String host, int port) { this.host = host; this.port = port; } /** * Runs the Web client * @throws IOException if a socket error occurs */ public void run() throws IOException { int contentLength = 0; // Open a socket to the Web host Socket socket = new Socket(host, port); // Set up to read input from the user // and echo it to Web host BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); // The first line is the HTTP request // e.g., "GET /path HTTP/1.0" String line = in.readLine(); out.println(line); // Read and echo any other headers, stopping // when a blank line is encountered. while ((line = in.readLine()) != null) { line = line.trim(); out.println(line); if (line.equals("")) break; // Check for a Content-Length header Pattern p = Pattern.compile ("^\s*([^:]+):\s+(.*\S)\s*$"); Matcher m = p.matcher(line); if (m.matches()) { String name = m.group(1); String value = m.group(2); if (name.equalsIgnoreCase("Content-Length")) contentLength = Integer.parseInt(value); } } // If a non-zero content length header was used, // read and echo that many bytes to the Web host. if (contentLength > 0) { for (int i = 0; i < contentLength; i++) out.print((char) in.read()); out.flush(); } // The server is now working on the request. // Read its output and dump to stdout in = new BufferedReader( new InputStreamReader(socket.getInputStream())); out = new PrintWriter(System.out); for (;;) { line = in.readLine(); if (line == null) break; out.println(line); } // Close files in.close(); out.close(); socket.close(); } } |
Cet article JSP: Exemple d’un Client Web est apparu en premier sur .