com.finwin.util
Class HTTPRequest

java.lang.Object
  |
  +--com.finwin.util.HTTPRequest

public class HTTPRequest
extends java.lang.Object

An object for making HTTP requests over TCP/IP. This object implements an HTTP request, including producting an HTTP/1.0 header and reading the response header. It will perform an HTTP post operation, allowing the program to specify keyed elements for the post.
Note: When using this object, the method close() must be called to release network resources heald by the object. If close() is not called memory leaks and network resource leaks may occur.


Inner Class Summary
 class HTTPRequest.pair
           
 
Field Summary
static int GET
           
static int POST
           
 
Constructor Summary
HTTPRequest(int method, java.net.URL url)
          Constructs the HTTP request secifying the method for the request and the url to contact.
 
Method Summary
 void addHeader(java.lang.String key, java.lang.String value)
          Adds an element to the request header.
 void addPostValue(java.lang.String key, java.lang.String value)
          Adds a value to the post request.
 void close()
          Releases the TCP/IP data in use by the request.
 java.lang.String getHeader(java.lang.String key)
          Attempts to locate a header entry.
 java.io.BufferedReader getInputReader()
          Returns the input stream from the request, so that large files may be read by buffered readers.
 java.lang.String getResponse()
          Reads the response and returns it as a string.
 int getResultCode()
          Obtains the result code.
 int request()
          Makes the request from the server.
static java.lang.String urlEnc(java.lang.String s)
          Encodes a string URL style.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GET

public static final int GET

POST

public static final int POST
Constructor Detail

HTTPRequest

public HTTPRequest(int method,
                   java.net.URL url)
Constructs the HTTP request secifying the method for the request and the url to contact. The value for method must come from the set {HTTPRequest.GET, HTTPRequest.POST}. The url must be a valid URL. Calling this method will default the request port
Method Detail

addHeader

public void addHeader(java.lang.String key,
                      java.lang.String value)
Adds an element to the request header.
Parameters:
key - The name of the header element
value - The value of the element to add to the request header

addPostValue

public void addPostValue(java.lang.String key,
                         java.lang.String value)
Adds a value to the post request. The object must have been created with HTTPRequest.POST passed as the parameter for method to the constructor, otherwise this method will throw an IllegalArgumentException
Parameters:
key - The name of the value to be posted with the request.
value - The value of the element to be posted with the request.
Throws:
java.lang.IllegalArgumentException - - If this object was not created as a "POST" request.

urlEnc

public static final java.lang.String urlEnc(java.lang.String s)
Encodes a string URL style.
Parameters:
s - The source value
Returns:
A URL encode string

getHeader

public java.lang.String getHeader(java.lang.String key)
Attempts to locate a header entry. If it is not found the method returns null.
Parameters:
key - The header key to locate
Returns:
null if the item is not found.

getResponse

public java.lang.String getResponse()
Reads the response and returns it as a string.
Returns:
the response body as a string.

getInputReader

public java.io.BufferedReader getInputReader()
Returns the input stream from the request, so that large files may be read by buffered readers.
Returns:
The input stream from the request.

getResultCode

public int getResultCode()
Obtains the result code. It is the same as the value returned from request()
See Also:
request()

close

public void close()
Releases the TCP/IP data in use by the request. Call this method when finished with the HTTPRequest.

request

public int request()
Makes the request from the server. This method returns the result code from the server. After calling this method, clients may obtain the response as a string by calling getResponse() or calling getInputReader() to get the response's input reader.
Returns:
The result code of the request.
See Also:
getResponse(), getInputReader()