org.lobobrowser.html.test

Class SimpleHttpRequest

Implemented Interfaces:
HttpRequest

public class SimpleHttpRequest
extends java.lang.Object
implements HttpRequest

The SimpleHttpRequest class implements the HttpRequest interface. The HttpRequest implementation provided by this class is simple, with no caching. It creates a new thread for each new asynchronous request.
Author:
J. H. S.

Field Summary

protected java.net.URLConnection
connection
The URLConnection is assigned to this field while it is ongoing.
protected String
requestMethod
protected String
requestPassword
protected String
requestUserName
protected String
responseHeaders
Response headers are set in this string after a response is received.
protected Map
responseHeadersMap
Response headers are set in this map after a response is received.

Fields inherited from interface org.lobobrowser.html.HttpRequest

STATE_COMPLETE, STATE_INTERACTIVE, STATE_LOADED, STATE_LOADING, STATE_UNINITIALIZED

Constructor Summary

SimpleHttpRequest(UserAgentContext context, java.net.Proxy proxy)

Method Summary

void
abort()
Aborts an ongoing request.
void
addReadyStateChangeListener(ReadyStateChangeListener listener)
Adds a listener of ReadyState changes.
String
getAllResponseHeaders()
Gets a string with all the response headers.
protected String
getPostCharset()
This is the charset used to post data provided to send(String).
int
getReadyState()
Gets the state of the request, a value between 0 and 4.
byte[]
getResponseBytes()
Gets the request response bytes.
String
getResponseHeader(String headerName)
Gets a response header value.
Image
getResponseImage()
Gets the request response as an AWT image.
String
getResponseText()
Gets the request response as text.
Document
getResponseXML()
Gets the request response as an XML DOM.
int
getStatus()
Gets the status of the response.
String
getStatusText()
Gets the status text of the request, e.g.
void
open(String method, String url)
Starts an asynchronous request.
void
open(String method, String url, boolean asyncFlag)
Opens a request.
void
open(String method, URL url)
Opens an asynchronous request.
void
open(String method, URL url, boolean asyncFlag)
Opens an request.
void
open(String method, java.net.URL url, boolean asyncFlag, String userName)
void
open(String method, java.net.URL url, boolean asyncFlag, String userName, String password)
Opens the request.
void
send(String content)
Sends POST content, if any, and causes the request to proceed.
protected void
sendSync(String content)
This is a synchronous implementation of send(String) method functionality.

Field Details

connection

protected java.net.URLConnection connection
The URLConnection is assigned to this field while it is ongoing.

requestMethod

protected String requestMethod

requestPassword

protected String requestPassword

requestUserName

protected String requestUserName

responseHeaders

protected String responseHeaders
Response headers are set in this string after a response is received.

responseHeadersMap

protected Map responseHeadersMap
Response headers are set in this map after a response is received.

Constructor Details

SimpleHttpRequest

public SimpleHttpRequest(UserAgentContext context,
                         java.net.Proxy proxy)

Method Details

abort

public void abort()
Aborts an ongoing request.
Specified by:
abort in interface HttpRequest

addReadyStateChangeListener

public void addReadyStateChangeListener(ReadyStateChangeListener listener)
Adds a listener of ReadyState changes. The listener should be invoked even in the case of errors.
Specified by:
addReadyStateChangeListener in interface HttpRequest
Parameters:
listener - An instanceof of ReadyStateChangeListener

getAllResponseHeaders

public String getAllResponseHeaders()
Gets a string with all the response headers.
Specified by:
getAllResponseHeaders in interface HttpRequest

getPostCharset

protected String getPostCharset()

getReadyState

public int getReadyState()
Gets the state of the request, a value between 0 and 4.
Specified by:
getReadyState in interface HttpRequest
Returns:
A value corresponding to one of the STATE* constants in this class.

getResponseBytes

public byte[] getResponseBytes()
Gets the request response bytes.
Specified by:
getResponseBytes in interface HttpRequest

getResponseHeader

public String getResponseHeader(String headerName)
Gets a response header value.
Specified by:
getResponseHeader in interface HttpRequest
Parameters:
headerName - The name of the header.

getResponseImage

public Image getResponseImage()
Gets the request response as an AWT image.
Specified by:
getResponseImage in interface HttpRequest

getResponseText

public String getResponseText()
Gets the request response as text.
Specified by:
getResponseText in interface HttpRequest

getResponseXML

public Document getResponseXML()
Gets the request response as an XML DOM.
Specified by:
getResponseXML in interface HttpRequest

getStatus

public int getStatus()
Gets the status of the response. Note that this can be 0 for file requests in addition to 200 for successful HTTP requests.
Specified by:
getStatus in interface HttpRequest

getStatusText

public String getStatusText()
Gets the status text of the request, e.g. "OK" for 200.
Specified by:
getStatusText in interface HttpRequest

open

public void open(String method,
                 String url)
            throws IOException
Starts an asynchronous request.
Specified by:
open in interface HttpRequest
Parameters:
method - The request method.
url - The destination URL.

open

public void open(String method,
                 String url,
                 boolean asyncFlag)
            throws IOException
Opens a request.
Specified by:
open in interface HttpRequest
Parameters:
method - The request method.
url - The destination URL.
asyncFlag - Whether the request should be asynchronous.

open

public void open(String method,
                 URL url)
            throws IOException
Opens an asynchronous request.
Specified by:
open in interface HttpRequest
Parameters:
method - The request method.
url - The destination URL.

open

public void open(String method,
                 URL url,
                 boolean asyncFlag)
            throws IOException
Opens an request.
Specified by:
open in interface HttpRequest
Parameters:
method - The request method.
url - The destination URL.
asyncFlag - Whether the request is asynchronous.

open

public void open(String method,
                 java.net.URL url,
                 boolean asyncFlag,
                 String userName)
            throws IOException

open

public void open(String method,
                 java.net.URL url,
                 boolean asyncFlag,
                 String userName,
                 String password)
            throws java.io.IOException
Parameters:
method - The request method.
url - The request URL.
asyncFlag - Whether the request should be asynchronous.
userName - The user name of the request (not supported.)
password - The password of the request (not supported.)

send

public void send(String content)
            throws java.io.IOException
Sends POST content, if any, and causes the request to proceed.

In the case of asynchronous requests, a new thread is created.

Specified by:
send in interface HttpRequest
Parameters:
content - POST content or null if there's no such content.

sendSync

protected void sendSync(String content)
            throws IOException
This is a synchronous implementation of send(String) method functionality. It may be overridden to change the behavior of the class.
Parameters:
content - POST content if any. It may be null.