com.marringtons.net.HTTP
Class Client

java.lang.Object
  extended bycom.marringtons.net.HTTP.Client

public class Client
extends Object

This class provides complete HTTP client functionality so that a Java program can act as a browser does in talking to a HTTP server. It can be used to Get and Post data to a HTTP server and collect the results for analysis. Multiple exchanges may use the same connection if it is still open.

 Writer writer = new StringWriter();
 Client client = new Client(1958, writer); // initialise with the port number
 class Closer implements Closeable
 	{
 		public void close()
 			{
 				System.out.println("Client Read Complete");
 			}
 	}
 client.onComplete(new Closeable());
 client.get("http://myserver.com/testcgi.cgi", 80, writer);
 if (client.isAlive())
 System.out.println("client is still retrieving data");
 if (client.result(60))
 	System.out.println("Client returned " + writer.toString());
 else if (client.timedOut())
 	System.err.println("Client timed out ");
 else
 	System.err.println("Client error: " + client.error());
 
 // or if you want the body as a string:
 client = new Client(1958);
 String result = client.body(); // waits to return the completed body
 

Author:
Paul Marrington

Constructor Summary
Client(int port)
          Create a client pointing to a server on the local machine for the defined port.
Client(int port, Writer fromServer)
          Create a client pointing to a server on the local machine for the defined port.
Client(String hostName, int port)
          Create a client pointing to a server on the named machine.
Client(String hostName, int port, Writer fromServer)
          Create a client pointing to a server on the named machine.
 
Method Summary
 void clear()
          Used by testing routines where client input is kept in a string to clear this input.
 void close()
          Close a persistent connection (one where each message had a length).
 void converse()
          Called when thread starts to do the real work.
 void get()
          Set up client to receive from server without first sending anything.
 void get(String url)
          Send a Get request to the server - synchronous.
 Header getHeader()
          Retrieve the header from the last get or post command.
 boolean isOpen()
          Is the client connection still open and ready for business.
 void post(String url, String data)
          Send a Post request to the server - synchronous.
 void setCharacterMode()
          Set the transfer from server to character at a time (instead of line).
 void setCharacterMode(boolean mode)
          Set the transfer from server to character at a time (instead of line).
 String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Client

public Client(int port)
Create a client pointing to a server on the local machine for the defined port. Results can be retrieved with body() method.

Parameters:
port - to open for connection

Client

public Client(int port,
              Writer fromServer)
Create a client pointing to a server on the local machine for the defined port.

Parameters:
port - to open for connection
fromServer - stream to write to with data recieved from the server

Client

public Client(String hostName,
              int port)
       throws UnknownHostException
Create a client pointing to a server on the named machine.

Parameters:
hostName - name or IP as a string
port - to open for connection
Throws:
UnknownHostException

Client

public Client(String hostName,
              int port,
              Writer fromServer)
       throws UnknownHostException
Create a client pointing to a server on the named machine.

Parameters:
hostName - name or IP as a string
port - to open for connection
fromServer - stream to write to with data recieved from the server
Throws:
UnknownHostException
Method Detail

get

public void get()
         throws IOException
Set up client to receive from server without first sending anything. Used by unit testing when the unit test code instigates a server push.

Throws:
IOException - on a socket error

get

public void get(String url)
         throws IOException
Send a Get request to the server - synchronous.

Parameters:
url - to ask server to retrieve
Throws:
IOException - on a socket error

post

public void post(String url,
                 String data)
          throws IOException
Send a Post request to the server - synchronous.

Parameters:
url - to ask server to action
data - of the post alreay encoded (i.e. a=b&c=d+e&f=g)
Throws:
IOException - on a socket error

converse

public void converse()
              throws IOException
Called when thread starts to do the real work.

Throws:
IOException - on a socket error

toString

public String toString()
Returns:
the contents of the response from the remote server.
See Also:
Object.toString()

close

public void close()
Close a persistent connection (one where each message had a length).


isOpen

public boolean isOpen()
Is the client connection still open and ready for business.

Returns:
true if client connection had remained open after exchange.

setCharacterMode

public void setCharacterMode()
Set the transfer from server to character at a time (instead of line). Only works for pages that do not have a specific length.


setCharacterMode

public void setCharacterMode(boolean mode)
Set the transfer from server to character at a time (instead of line). Only works for pages that do not have a specific length.

Parameters:
mode - - true for character-at-a-time and false for line-at-a-time

getHeader

public Header getHeader()
Retrieve the header from the last get or post command.

Returns:
Header

clear

public void clear()
Used by testing routines where client input is kept in a string to clear this input. A subsequent toString() or body() will return an empty string.



Copyright © 2005 Paul Marrington http://library.marringtons.com