Package org.lobobrowser.html.test
Class SimpleHttpRequest
- java.lang.Object
-
- org.lobobrowser.html.test.SimpleHttpRequest
-
- All Implemented Interfaces:
HttpRequest
public class SimpleHttpRequest extends java.lang.Object implements HttpRequest
TheSimpleHttpRequest
class implements theHttpRequest
interface. TheHttpRequest
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
Fields Modifier and Type Field Description protected java.net.URLConnection
connection
TheURLConnection
is assigned to this field while it is ongoing.protected java.lang.String
requestMethod
protected java.lang.String
requestPassword
protected java.lang.String
requestUserName
protected java.lang.String
responseHeaders
Response headers are set in this string after a response is received.protected java.util.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
Constructors Constructor Description SimpleHttpRequest(UserAgentContext context, java.net.Proxy proxy)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
abort()
Aborts an ongoing request.void
addReadyStateChangeListener(ReadyStateChangeListener listener)
Adds a listener of ReadyState changes.java.lang.String
getAllResponseHeaders()
Gets a string with all the response headers.protected java.lang.String
getPostCharset()
This is the charset used to post data provided tosend(String)
.int
getReadyState()
Gets the state of the request, a value between 0 and 4.byte[]
getResponseBytes()
Gets the request response bytes.java.lang.String
getResponseHeader(java.lang.String headerName)
Gets a response header value.java.awt.Image
getResponseImage()
Gets the request response as an AWT image.java.lang.String
getResponseText()
Gets the request response as text.org.w3c.dom.Document
getResponseXML()
Gets the request response as an XML DOM.int
getStatus()
Gets the status of the response.java.lang.String
getStatusText()
Gets the status text of the request, e.g.void
open(java.lang.String method, java.lang.String url)
Starts an asynchronous request.void
open(java.lang.String method, java.lang.String url, boolean asyncFlag)
Opens a request.void
open(java.lang.String method, java.net.URL url)
Opens an asynchronous request.void
open(java.lang.String method, java.net.URL url, boolean asyncFlag)
Opens an request.void
open(java.lang.String method, java.net.URL url, boolean asyncFlag, java.lang.String userName)
Opens a request.void
open(java.lang.String method, java.net.URL url, boolean asyncFlag, java.lang.String userName, java.lang.String password)
Opens the request.void
send(java.lang.String content)
Sends POST content, if any, and causes the request to proceed.protected void
sendSync(java.lang.String content)
This is a synchronous implementation ofsend(String)
method functionality.
-
-
-
Field Detail
-
requestMethod
protected java.lang.String requestMethod
-
requestUserName
protected java.lang.String requestUserName
-
requestPassword
protected java.lang.String requestPassword
-
connection
protected java.net.URLConnection connection
TheURLConnection
is assigned to this field while it is ongoing.
-
responseHeadersMap
protected java.util.Map responseHeadersMap
Response headers are set in this map after a response is received.
-
responseHeaders
protected java.lang.String responseHeaders
Response headers are set in this string after a response is received.
-
-
Constructor Detail
-
SimpleHttpRequest
public SimpleHttpRequest(UserAgentContext context, java.net.Proxy proxy)
-
-
Method Detail
-
getReadyState
public int getReadyState()
Description copied from interface:HttpRequest
Gets the state of the request, a value between 0 and 4.- Specified by:
getReadyState
in interfaceHttpRequest
- Returns:
- A value corresponding to one of the STATE* constants in this class.
-
getResponseText
public java.lang.String getResponseText()
Description copied from interface:HttpRequest
Gets the request response as text.- Specified by:
getResponseText
in interfaceHttpRequest
-
getResponseXML
public org.w3c.dom.Document getResponseXML()
Description copied from interface:HttpRequest
Gets the request response as an XML DOM.- Specified by:
getResponseXML
in interfaceHttpRequest
-
getResponseBytes
public byte[] getResponseBytes()
Description copied from interface:HttpRequest
Gets the request response bytes.- Specified by:
getResponseBytes
in interfaceHttpRequest
-
getResponseImage
public java.awt.Image getResponseImage()
Description copied from interface:HttpRequest
Gets the request response as an AWT image.- Specified by:
getResponseImage
in interfaceHttpRequest
-
getStatus
public int getStatus()
Description copied from interface:HttpRequest
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 interfaceHttpRequest
-
getStatusText
public java.lang.String getStatusText()
Description copied from interface:HttpRequest
Gets the status text of the request, e.g. "OK" for 200.- Specified by:
getStatusText
in interfaceHttpRequest
-
abort
public void abort()
Description copied from interface:HttpRequest
Aborts an ongoing request.- Specified by:
abort
in interfaceHttpRequest
-
getAllResponseHeaders
public java.lang.String getAllResponseHeaders()
Description copied from interface:HttpRequest
Gets a string with all the response headers.- Specified by:
getAllResponseHeaders
in interfaceHttpRequest
-
getResponseHeader
public java.lang.String getResponseHeader(java.lang.String headerName)
Description copied from interface:HttpRequest
Gets a response header value.- Specified by:
getResponseHeader
in interfaceHttpRequest
- Parameters:
headerName
- The name of the header.
-
open
public void open(java.lang.String method, java.lang.String url) throws java.io.IOException
Description copied from interface:HttpRequest
Starts an asynchronous request.- Specified by:
open
in interfaceHttpRequest
- Parameters:
method
- The request method.url
- The destination URL.- Throws:
java.io.IOException
-
open
public void open(java.lang.String method, java.net.URL url) throws java.io.IOException
Description copied from interface:HttpRequest
Opens an asynchronous request.- Specified by:
open
in interfaceHttpRequest
- Parameters:
method
- The request method.url
- The destination URL.- Throws:
java.io.IOException
-
open
public void open(java.lang.String method, java.net.URL url, boolean asyncFlag) throws java.io.IOException
Description copied from interface:HttpRequest
Opens an request.- Specified by:
open
in interfaceHttpRequest
- Parameters:
method
- The request method.url
- The destination URL.asyncFlag
- Whether the request is asynchronous.- Throws:
java.io.IOException
-
open
public void open(java.lang.String method, java.lang.String url, boolean asyncFlag) throws java.io.IOException
Description copied from interface:HttpRequest
Opens a request.- Specified by:
open
in interfaceHttpRequest
- Parameters:
method
- The request method.url
- The destination URL.asyncFlag
- Whether the request should be asynchronous.- Throws:
java.io.IOException
-
open
public void open(java.lang.String method, java.net.URL url, boolean asyncFlag, java.lang.String userName) throws java.io.IOException
Description copied from interface:HttpRequest
Opens a request.- Specified by:
open
in interfaceHttpRequest
- Parameters:
method
- The request method.url
- The destination URL.asyncFlag
- Whether the request should be asynchronous.userName
- The HTTP authentication user name.- Throws:
java.io.IOException
-
open
public void open(java.lang.String method, java.net.URL url, boolean asyncFlag, java.lang.String userName, java.lang.String password) throws java.io.IOException
Opens the request. Callsend(String)
to complete it.- Specified by:
open
in interfaceHttpRequest
- 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.)- Throws:
java.io.IOException
-
send
public void send(java.lang.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 interfaceHttpRequest
- Parameters:
content
- POST content ornull
if there's no such content.- Throws:
java.io.IOException
-
getPostCharset
protected java.lang.String getPostCharset()
This is the charset used to post data provided tosend(String)
. It returns "UTF-8" unless overridden.
-
sendSync
protected void sendSync(java.lang.String content) throws java.io.IOException
This is a synchronous implementation ofsend(String)
method functionality. It may be overridden to change the behavior of the class.- Parameters:
content
- POST content if any. It may benull
.- Throws:
java.io.IOException
-
addReadyStateChangeListener
public void addReadyStateChangeListener(ReadyStateChangeListener listener)
Description copied from interface:HttpRequest
Adds a listener of ReadyState changes. The listener should be invoked even in the case of errors.- Specified by:
addReadyStateChangeListener
in interfaceHttpRequest
- Parameters:
listener
- An instanceof ofReadyStateChangeListener
-
-