Package org.htmlunit.javascript.host.xml
Class XMLHttpRequest
- java.lang.Object
-
- org.htmlunit.corejs.javascript.ScriptableObject
-
- org.htmlunit.javascript.HtmlUnitScriptable
-
- org.htmlunit.javascript.host.event.EventTarget
-
- org.htmlunit.javascript.host.xml.XMLHttpRequestEventTarget
-
- org.htmlunit.javascript.host.xml.XMLHttpRequest
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,org.htmlunit.corejs.javascript.ConstProperties,org.htmlunit.corejs.javascript.debug.DebuggableObject,org.htmlunit.corejs.javascript.Scriptable,org.htmlunit.corejs.javascript.SymbolScriptable
public class XMLHttpRequest extends XMLHttpRequestEventTarget
A JavaScript object for anXMLHttpRequest.- See Also:
- W3C XMLHttpRequest, Safari documentation, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classXMLHttpRequest.NetworkErrorWebResponseprivate static classXMLHttpRequest.NoPermittedHeaderException
-
Field Summary
Fields Modifier and Type Field Description private booleanaborted_private static java.lang.String[]ALL_PROPERTIES_private static java.lang.StringALLOW_ORIGIN_ALLprivate booleanasync_private booleancaseSensitiveProperties_static intDONEAll the data has been received; the complete data is available in responseBody and responseText.static intHEADERS_RECEIVEDThe send() method has been called, but the status and headers are not yet available.private booleanisSameOrigin_private intjobID_static intLOADINGSome data has been received.private static org.apache.commons.logging.LogLOGstatic intOPENEDThe object has been created, but the send() method has not been called.private java.lang.StringoverriddenMimeType_private static java.util.HashSet<java.lang.String>PROHIBITED_HEADERS_private static java.lang.StringRESPONSE_TYPE_ARRAYBUFFERprivate static java.lang.StringRESPONSE_TYPE_BLOBprivate static java.lang.StringRESPONSE_TYPE_DEFAULTprivate static java.lang.StringRESPONSE_TYPE_DOCUMENTprivate static java.lang.StringRESPONSE_TYPE_JSONprivate static java.lang.StringRESPONSE_TYPE_TEXTprivate java.lang.StringresponseType_private intstate_private inttimeout_static intUNSENTThe object has been created, but not initialized (the open() method has not been called).private WebRequestwebRequest_private WebResponsewebResponse_private booleanwithCredentials_
-
Constructor Summary
Constructors Constructor Description XMLHttpRequest()Creates a new instance.XMLHttpRequest(boolean caseSensitiveProperties)Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidabort()Cancels the current HTTP request.(package private) voiddoSend()The real send job.private voidfireJavascriptEvent(java.lang.String eventName)private voidfireJavascriptEventIgnoreAbort(java.lang.String eventName)java.lang.Objectget(java.lang.String name, org.htmlunit.corejs.javascript.Scriptable start)Gets a named property from the object.java.lang.StringgetAllResponseHeaders()Returns the labels and values of all the HTTP headers.org.htmlunit.corejs.javascript.FunctiongetOnreadystatechange()Returns the event handler that fires on ready state change.intgetReadyState()Returns the current state of the HTTP request.java.lang.ObjectgetResponse()java.lang.StringgetResponseHeader(java.lang.String headerName)Retrieves the value of an HTTP header from the response body.java.lang.StringgetResponseText()Returns a string version of the data retrieved from the server.java.lang.StringgetResponseType()java.lang.ObjectgetResponseXML()Returns a DOM-compatible document object version of the data retrieved from the server.intgetStatus()Returns the numeric status returned by the server, such as 404 for "Not Found" or 200 for "OK".java.lang.StringgetStatusText()Returns the string message accompanying the status code, such as "Not Found" or "OK".intgetTimeout()XMLHttpRequestUploadgetUpload()Returns theuploadproperty.(package private) static booleanisAuthorizedHeader(java.lang.String name)Not all request headers can be set from JavaScript.private booleanisPreflight()private booleanisPreflightAuthorized(WebResponse preflightResponse)private static booleanisPreflightHeader(java.lang.String name, java.lang.String value)private static booleanisSameOrigin(java.net.URL originUrl, java.net.URL newUrl)booleanisWithCredentials()Returns thewithCredentialsproperty.voidjsConstructor()JavaScript constructor.voidopen(java.lang.String method, java.lang.Object urlParam, java.lang.Object asyncParam, java.lang.Object user, java.lang.Object password)Assigns the destination URL, method and other optional attributes of a pending request.voidoverrideMimeType(java.lang.String mimeType)Override the mime type returned by the server (if any).private voidprepareRequestContent(java.lang.Object content)Prepares the WebRequest that will be sent.voidput(java.lang.String name, org.htmlunit.corejs.javascript.Scriptable start, java.lang.Object value)voidsend(java.lang.Object content)Sends the specified content to the server in an HTTP request and receives the response.voidsetOnreadystatechange(org.htmlunit.corejs.javascript.Function readyStateChangeHandler)Sets the event handler that fires on ready state change.voidsetRequestHeader(java.lang.String name, java.lang.String value)Sets the specified header to the specified value.voidsetResponseType(java.lang.String responseType)Sets theresponseTypeproperty.private voidsetState(int state)Sets the state as specified and invokes the state change handler if one has been set.voidsetTimeout(int timeout)voidsetWithCredentials(boolean withCredentials)Sets thewithCredentialsproperty.-
Methods inherited from class org.htmlunit.javascript.host.xml.XMLHttpRequestEventTarget
getOnabort, getOnerror, getOnload, getOnloadend, getOnloadstart, getOnprogress, getOntimeout, setOnabort, setOnerror, setOnload, setOnloadend, setOnloadstart, setOnprogress, setOntimeout
-
Methods inherited from class org.htmlunit.javascript.host.event.EventTarget
addEventListener, clearEventListenersContainer, dispatchEvent, executeEventLocally, fireEvent, getEventHandler, getEventListenersContainer, hasEventHandlers, isEventHandlerOnWindow, removeEventListener, setEventHandler
-
Methods inherited from class org.htmlunit.javascript.HtmlUnitScriptable
clone, equivalentValues, getBrowserVersion, getClassName, getDefaultValue, getDomNodeOrDie, getDomNodeOrNull, getPrototype, getScriptableFor, getStartingScope, getWindow, getWindow, getWithPreemption, has, hasInstance, initParentScope, makeScriptableFor, setClassName, setDomNode, setDomNode, setParentScope, setupPromise, setupRejectedPromise
-
Methods inherited from class org.htmlunit.corejs.javascript.ScriptableObject
applyDescriptorToAttributeBitset, associateValue, avoidObjectDetection, buildDataDescriptor, callMethod, callMethod, checkPropertyChange, checkPropertyDefinition, defineClass, defineClass, defineClass, defineConst, defineConstProperty, defineFunctionProperties, defineOwnProperties, defineOwnProperty, defineOwnProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, delete, delete, delete, deleteProperty, deleteProperty, deleteProperty, ensureScriptable, ensureScriptableObject, ensureSymbolScriptable, get, get, get, getAllIds, getArrayPrototype, getAssociatedValue, getAttributes, getAttributes, getAttributes, getAttributes, getAttributes, getClassPrototype, getDefaultValue, getExternalArrayData, getExternalArrayLength, getFunctionPrototype, getGeneratorFunctionPrototype, getGetterOrSetter, getGetterOrSetter, getIds, getObjectPrototype, getOwnPropertyDescriptor, getParentScope, getProperty, getProperty, getProperty, getPropertyIds, getPrototype, getTopLevelScope, getTopScopeValue, getTypedProperty, getTypedProperty, getTypeOf, has, has, hasProperty, hasProperty, hasProperty, isAccessorDescriptor, isConst, isDataDescriptor, isEmpty, isExtensible, isFalse, isGenericDescriptor, isGetterOrSetter, isSealed, isTrue, preventExtensions, put, put, putConst, putConstProperty, putProperty, putProperty, putProperty, querySlot, redefineProperty, sameValue, sealObject, setAttributes, setAttributes, setAttributes, setAttributes, setAttributes, setCommonDescriptorProperties, setExternalArrayData, setGetterOrSetter, setPrototype, size
-
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
UNSENT
public static final int UNSENT
The object has been created, but not initialized (the open() method has not been called).- See Also:
- Constant Field Values
-
OPENED
public static final int OPENED
The object has been created, but the send() method has not been called.- See Also:
- Constant Field Values
-
HEADERS_RECEIVED
public static final int HEADERS_RECEIVED
The send() method has been called, but the status and headers are not yet available.- See Also:
- Constant Field Values
-
LOADING
public static final int LOADING
Some data has been received.- See Also:
- Constant Field Values
-
DONE
public static final int DONE
All the data has been received; the complete data is available in responseBody and responseText.- See Also:
- Constant Field Values
-
RESPONSE_TYPE_DEFAULT
private static final java.lang.String RESPONSE_TYPE_DEFAULT
- See Also:
- Constant Field Values
-
RESPONSE_TYPE_ARRAYBUFFER
private static final java.lang.String RESPONSE_TYPE_ARRAYBUFFER
- See Also:
- Constant Field Values
-
RESPONSE_TYPE_BLOB
private static final java.lang.String RESPONSE_TYPE_BLOB
- See Also:
- Constant Field Values
-
RESPONSE_TYPE_DOCUMENT
private static final java.lang.String RESPONSE_TYPE_DOCUMENT
- See Also:
- Constant Field Values
-
RESPONSE_TYPE_JSON
private static final java.lang.String RESPONSE_TYPE_JSON
- See Also:
- Constant Field Values
-
RESPONSE_TYPE_TEXT
private static final java.lang.String RESPONSE_TYPE_TEXT
- See Also:
- Constant Field Values
-
ALLOW_ORIGIN_ALL
private static final java.lang.String ALLOW_ORIGIN_ALL
- See Also:
- Constant Field Values
-
ALL_PROPERTIES_
private static final java.lang.String[] ALL_PROPERTIES_
-
PROHIBITED_HEADERS_
private static final java.util.HashSet<java.lang.String> PROHIBITED_HEADERS_
-
state_
private int state_
-
webRequest_
private WebRequest webRequest_
-
async_
private boolean async_
-
jobID_
private int jobID_
-
webResponse_
private WebResponse webResponse_
-
overriddenMimeType_
private java.lang.String overriddenMimeType_
-
caseSensitiveProperties_
private final boolean caseSensitiveProperties_
-
withCredentials_
private boolean withCredentials_
-
isSameOrigin_
private boolean isSameOrigin_
-
timeout_
private int timeout_
-
aborted_
private boolean aborted_
-
responseType_
private java.lang.String responseType_
-
-
Method Detail
-
jsConstructor
public void jsConstructor()
JavaScript constructor.- Overrides:
jsConstructorin classXMLHttpRequestEventTarget
-
setState
private void setState(int state)
Sets the state as specified and invokes the state change handler if one has been set.- Parameters:
state- the new state
-
fireJavascriptEvent
private void fireJavascriptEvent(java.lang.String eventName)
-
fireJavascriptEventIgnoreAbort
private void fireJavascriptEventIgnoreAbort(java.lang.String eventName)
-
getReadyState
public int getReadyState()
Returns the current state of the HTTP request. The possible values are:- 0 = unsent
- 1 = opened
- 2 = headers_received
- 3 = loading
- 4 = done
- Returns:
- the current state of the HTTP request
-
getResponseType
public java.lang.String getResponseType()
- Returns:
- the
responseTypeproperty
-
setResponseType
public void setResponseType(java.lang.String responseType)
Sets theresponseTypeproperty.- Parameters:
responseType- theresponseTypeproperty.
-
getResponse
public java.lang.Object getResponse()
- Returns:
- returns the response's body content as an ArrayBuffer, Blob, Document, JavaScript Object, or DOMString, depending on the value of the request's responseType property.
-
getResponseText
public java.lang.String getResponseText()
Returns a string version of the data retrieved from the server.- Returns:
- a string version of the data retrieved from the server
-
getResponseXML
public java.lang.Object getResponseXML()
Returns a DOM-compatible document object version of the data retrieved from the server.- Returns:
- a DOM-compatible document object version of the data retrieved from the server
-
getStatus
public int getStatus()
Returns the numeric status returned by the server, such as 404 for "Not Found" or 200 for "OK".- Returns:
- the numeric status returned by the server
-
getStatusText
public java.lang.String getStatusText()
Returns the string message accompanying the status code, such as "Not Found" or "OK".- Returns:
- the string message accompanying the status code
-
abort
public void abort()
Cancels the current HTTP request.
-
getAllResponseHeaders
public java.lang.String getAllResponseHeaders()
Returns the labels and values of all the HTTP headers.- Returns:
- the labels and values of all the HTTP headers
-
getResponseHeader
public java.lang.String getResponseHeader(java.lang.String headerName)
Retrieves the value of an HTTP header from the response body.- Parameters:
headerName- the (case-insensitive) name of the header to retrieve- Returns:
- the value of the specified HTTP header
-
open
public void open(java.lang.String method, java.lang.Object urlParam, java.lang.Object asyncParam, java.lang.Object user, java.lang.Object password)Assigns the destination URL, method and other optional attributes of a pending request.- Parameters:
method- the method to use to send the request to the server (GET, POST, etc)urlParam- the URL to send the request toasyncParam- Whether or not to send the request to the server asynchronously, defaults totrueuser- If authentication is needed for the specified URL, the username to use to authenticatepassword- If authentication is needed for the specified URL, the password to use to authenticate
-
isSameOrigin
private static boolean isSameOrigin(java.net.URL originUrl, java.net.URL newUrl)
-
send
public void send(java.lang.Object content)
Sends the specified content to the server in an HTTP request and receives the response.- Parameters:
content- the body of the message being sent with the request
-
prepareRequestContent
private void prepareRequestContent(java.lang.Object content)
Prepares the WebRequest that will be sent.- Parameters:
content- the content to send
-
doSend
void doSend()
The real send job.
-
isPreflight
private boolean isPreflight()
-
isPreflightAuthorized
private boolean isPreflightAuthorized(WebResponse preflightResponse)
-
isPreflightHeader
private static boolean isPreflightHeader(java.lang.String name, java.lang.String value)- Parameters:
name- header name (MUST be lower-case for performance reasons)value- header value
-
setRequestHeader
public void setRequestHeader(java.lang.String name, java.lang.String value)Sets the specified header to the specified value. Theopenmethod must be called before this method, or an error will occur.- Parameters:
name- the name of the header being setvalue- the value of the header being set
-
isAuthorizedHeader
static boolean isAuthorizedHeader(java.lang.String name)
Not all request headers can be set from JavaScript.- Parameters:
name- the header name- Returns:
trueif the header can be set from JavaScript- See Also:
- W3C doc
-
overrideMimeType
public void overrideMimeType(java.lang.String mimeType)
Override the mime type returned by the server (if any). This may be used, for example, to force a stream to be treated and parsed as text/xml, even if the server does not report it as such. This must be done before the send method is invoked.- Parameters:
mimeType- the type used to override that returned by the server (if any)- See Also:
- XUL Planet
-
isWithCredentials
public boolean isWithCredentials()
Returns thewithCredentialsproperty.- Returns:
- the
withCredentialsproperty
-
setWithCredentials
public void setWithCredentials(boolean withCredentials)
Sets thewithCredentialsproperty.- Parameters:
withCredentials- thewithCredentialsproperty.
-
get
public java.lang.Object get(java.lang.String name, org.htmlunit.corejs.javascript.Scriptable start)Gets a named property from the object. Normally HtmlUnit objects don't need to overwrite this method as properties are defined on the prototypes from the XML configuration. In some cases where "content" of object has priority compared to the properties consider using utilityHtmlUnitScriptable.getWithPreemption(String).- Specified by:
getin interfaceorg.htmlunit.corejs.javascript.Scriptable- Overrides:
getin classHtmlUnitScriptable
-
put
public void put(java.lang.String name, org.htmlunit.corejs.javascript.Scriptable start, java.lang.Object value)- Specified by:
putin interfaceorg.htmlunit.corejs.javascript.Scriptable- Overrides:
putin classHtmlUnitScriptable
-
getUpload
public XMLHttpRequestUpload getUpload()
Returns theuploadproperty.- Returns:
- the
uploadproperty
-
getOnreadystatechange
public org.htmlunit.corejs.javascript.Function getOnreadystatechange()
Returns the event handler that fires on ready state change.- Overrides:
getOnreadystatechangein classXMLHttpRequestEventTarget- Returns:
- the event handler that fires on ready state change
-
setOnreadystatechange
public void setOnreadystatechange(org.htmlunit.corejs.javascript.Function readyStateChangeHandler)
Sets the event handler that fires on ready state change.- Overrides:
setOnreadystatechangein classXMLHttpRequestEventTarget- Parameters:
readyStateChangeHandler- the event handler that fires on ready state change
-
getTimeout
public int getTimeout()
-
setTimeout
public void setTimeout(int timeout)
-
-