001    /*
002     * Copyright 2000-2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016     
017    package org.apache.commons.scaffold.http;
018    
019    import java.net.*;
020    import java.io.*;
021    
022    
023    /**
024     * A network client that reads from a HTTP URL.
025     * <P>
026     * Taken from Core Servlets and JavaServer Pages
027     * from Prentice Hall and Sun Microsystems Press,
028     * http://www.coreservlets.com/.
029     * &copy; 2000 Marty Hall; may be freely used or adapted.
030     * <P>
031     * Adapted for general use by a servlet to read a page
032     * into a StringBuffer by Ted Husted, August 2001.
033     *
034     * @author Marty Hall
035     * @author Ted Husted
036     * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $
037     */
038    public class HttpClient {
039    
040        /**
041         * Retrieve indicated page, returning headers and
042         * page content.
043         *
044         * @param host
045         * @param port
046         * @param requestLine
047         * @param requestHeaders
048         * @param content
049         * @exception Catches IOException and UnknownHostException,
050         * and returns messages in content.
051         * @author Marty Hall
052         * @author Ted Husted
053         * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $
054         */
055        public HttpClient(
056                String host, int port,
057                String requestLine, String[] requestHeaders,
058                StringBuffer responseHeaders, StringBuffer responseContent)
059                    throws UnknownHostException,IOException {
060    
061                // check host and connect - Will succeed or throw UHE
062            InetAddress.getByName(host);
063            Socket uriSocket = new Socket(host,port);
064    
065                // handleConnection - May throw IOE
066            PrintWriter out = SocketUtil.getWriter(uriSocket);
067            BufferedReader in = SocketUtil.getReader(uriSocket);
068    
069                // say howdy
070            out.println(requestLine);
071    
072                // send any headers, and then blank
073            for(int i=0; i<requestHeaders.length; i++) {
074                if (requestHeaders[i] == null)
075                    break;
076                else
077                    out.println(requestHeaders[i]);
078            }
079            out.println();
080    
081                // capture response
082            String line;
083               // - headers
084            while ((line = in.readLine()) != null) {
085                responseHeaders.append(line + "\n");
086                if ("".equals(line)) break;
087            }
088                // - content
089            while ((line = in.readLine()) != null)
090                responseContent.append(line + "\n");
091       }
092    }