package HttpServletExamples;

import java.io.*;
import java.net.*;

/**
 * A very simple http web-page reading example that won't work in some circumstances.
 * But it will in some others.
 * 
 * @author mcgredo
 * @author brutzman@nps.edu
 */
public class HttpWebPageSource
{
    /** Default constructor */
    public HttpWebPageSource()
    {
        // default constructor
    }
    /**
     * Program invocation, execution starts here
     * @param args command-line arguments
     */
    public static void main(String[] args)
    {
        try
        {
           System.out.println("HttpWebPageSource: create http connection and retrieve default page");
           System.out.println("Reference:  https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol");
           System.out.println("Reference:  https://tools.ietf.org/html/rfc7230");
           System.out.println("Reference:  https://en.wikipedia.org/wiki/CURL");
           System.out.println();
           
           // We request an IP to connect to a web server running on default http port 80.
           
           String WEB_SERVER_ADDRESS    = "www.nps.edu";
           int    WEB_SERVER_PORT_HTTP  = 80;
           int    WEB_SERVER_PORT_HTTPS = 443; // does this work too?
           System.out.println("New socket WEB_ADDRESS=" + WEB_SERVER_ADDRESS);
           
           // this Java construct will work for HTTP but not HTTPS
           Socket socket = new Socket(WEB_SERVER_ADDRESS, WEB_SERVER_PORT_HTTPS); // compare alternative: https on port 443
           
           // we send a command to the web server, asking for what
           // we want. Note that the format for the command is very
           // specific and documented.
           OutputStream os = socket.getOutputStream();
           PrintStream  ps = new PrintStream(os);
		   
		   String message = "GET /index.html HTTP/1.0";
           ps.print(message); // to socket
           System.out.println(message);
           System.out.println();
           
           // Commands have to terminate with "carriage return/line feed"
           // twice to end the request. Those are the special characters
           // to have the control characters printed.  Part of the http protocol!
           ps.print("\r\n\r\n");
           ps.flush();
           
           // Up until now we have been reading one line only.
           // But a web server will write an unknown number of lines.
           // So now we read until the transmisson ends.
           
           InputStream    is = socket.getInputStream();
           Reader         isr = new InputStreamReader(is);
           BufferedReader br = new BufferedReader(isr);
           
           String line;
           int count = 0;
           
           while((line = br.readLine()) != null) // read from BufferedReader br one line at a time
           {
               count++;
               System.out.println(count + ": " + line);
           }
           System.out.println("HttpWebPageSource: response finished");
        }
        catch(IOException e)
        {
            System.err.println("HttpWebPageSource: problem with client");
            System.err.println(e);
        }
    }
}