
import java.net.*;
import java.io.*;
import java.util.*;
import javax.net.ssl.*;
import java.security.Security;

public class OldHTTPWrapper
{

    public static final int SOCKET_TIMEOUT_VALUE = 120 * 1000;

    public static int PORT = 0;
    public static String IP = null;
    
    public Socket s = null;
    public PrintWriter out = null;
    public InputStream is = null;
    public SSLSocketFactory factory = null;
           



    public static void main(String args[])
    {

        if (args[0] != null)
        {
            IP = args[0];
        }

        if (args[1] != null)
        {
            String tempPort = args[1];
            PORT = Integer.valueOf(tempPort).intValue();
        }

        boolean showPage = false;

        if (args.length > 2)
        {
          if (args[2] != null)
           {
            showPage=true;
           }

        }

		// Disable Cert checking (dont use for real stuff, for test only)
        Security.setProperty( "ssl.SocketFactory.provider","DummySSLSocketFactory");

        String url="/";

        OldHTTPWrapper httpWrapper = new OldHTTPWrapper();
        
        	String page = httpWrapper.getPage(url);
        
        	if(showPage)
        	{
        		System.out.println(page);
        	}

        	if (page.indexOf("HTTP/1.1 200 OK") != -1)
        	{
         		System.out.print(".");
        	}
        	else
        	{
         		System.out.println("received failure at " + new Date());
         		System.out.println(" page is: **************************");
         		System.out.println(page);
            }

    } // end main



     //Initialize instance data
    public void initialize(String theHost, int thePort)
    {
        // for ssl only
        IP = theHost;
        PORT = thePort;
    }
    

    public String getPage(String url)
    {
        int bcount = 0;
        StringBuffer sb = new StringBuffer();

        try
        {
            factory = (DummySSLSocketFactory)DummySSLSocketFactory.getDefault();

            Socket s = null;
            s= factory.createSocket(IP,PORT);
            s.setSoTimeout(SOCKET_TIMEOUT_VALUE);

            PrintWriter out = new PrintWriter(s.getOutputStream());

            String output = ("GET " + url
                             + " HTTP/1.0\r\n"
                             + "User-Agent: Java1.2.2\r\n"
                             + "Host: " + IP +":"+PORT + "\r\n"
                             // + "Connection: Keep-Alive" + "\r\n"
                             + "Accept: */*\r\n\r\n");
            out.print(output);
            out.flush();

            DataInputStream is = new DataInputStream(new BufferedInputStream(s.getInputStream()));

            int thechar;
            
            while ((thechar=is.read())!=-1)
            {
                bcount = bcount +1;
                sb.append((char)thechar);
            }
               
            is.close();
            out.close();
            s.close();  // very important

            
        } catch (Exception e)
        {
          e.printStackTrace();
        }
        
      return sb.toString();
   
    }


}

