|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.marringtons.net.HTTP.Client
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
| 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 |
public Client(int port)
port - to open for connection
public Client(int port,
Writer fromServer)
port - to open for connectionfromServer - stream to write to with data recieved from the server
public Client(String hostName,
int port)
throws UnknownHostException
hostName - name or IP as a stringport - to open for connection
UnknownHostException
public Client(String hostName,
int port,
Writer fromServer)
throws UnknownHostException
hostName - name or IP as a stringport - to open for connectionfromServer - stream to write to with data recieved from the server
UnknownHostException| Method Detail |
public void get()
throws IOException
IOException - on a socket error
public void get(String url)
throws IOException
url - to ask server to retrieve
IOException - on a socket error
public void post(String url,
String data)
throws IOException
url - to ask server to actiondata - of the post alreay encoded (i.e. a=b&c=d+e&f=g)
IOException - on a socket error
public void converse()
throws IOException
IOException - on a socket errorpublic String toString()
Object.toString()public void close()
public boolean isOpen()
public void setCharacterMode()
public void setCharacterMode(boolean mode)
mode - - true for character-at-a-time and false for line-at-a-timepublic Header getHeader()
public void clear()
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||