DOCUMENTS 5 - PortalScripting API
|
The XMLHTTPRequest class represents a HTTP request. More...
Public Member Functions | |
boolean | abort () |
Abort an asynchronous request if it already has been sent. More... | |
boolean | addRequestHeader (String name, String value) |
Add a HTTP header to the request to be sent. More... | |
String | getAllResponseHeaders () |
Get all response headers as a string. More... | |
String | getResponseHeader (String name) |
Get the value of the specified response header. More... | |
boolean | open (String method, String url, boolean async=false, String user="", String passwd="") |
Initialize a HTTP request. More... | |
boolean | send (String content="") |
Send the request to the HTTP server. More... | |
void | setCAInfo (String pemFile, int options) |
Set one or more certificates to verify the peer if using HTTPS. More... | |
XMLHTTPRequest | XMLHTTPRequest (String proxy="", int proxyPort=3128, String proxyUser="", String proxyPasswd="") |
Create a new XMLHTTPRequest object. More... | |
XMLHTTPRequest | XMLHTTPRequest (bool useProxySetting=true) |
Create a new XMLHTTPRequest object with option to switch off the query mechanism for proxy settings. More... | |
Public Attributes | |
boolean | canAsync |
Flag indicating whether asynchronous requests are possible on the used plattform. More... | |
boolean | canProxy |
Flag indicating whether the implementation on the used plattform allows the direct specifying of a proxy server. More... | |
int | COMPLETED |
The constant 4 for XMLHTTPRequest.readyState. More... | |
int | FileSizeHint |
Optional File size indicator for sending pure sequential files. More... | |
int | INTERACTIVE |
The constant 3 for XMLHTTPRequest.readyState. More... | |
int | NOTSENT |
The constant 1 for XMLHTTPRequest.readyState. More... | |
int | readyState |
The current state of the asynchronous request. More... | |
Var | response |
The response received from the HTTP server or null if no response is received. More... | |
File | responseFile |
An optional writable file for streaming a large response. More... | |
String | responseType |
Preferred output format of the response property (optional). More... | |
int | SENT |
The constant 2 for XMLHTTPRequest.readyState. More... | |
int | status |
The HTTP status code of the request. More... | |
String | statusText |
The HTTP status text of the request. More... | |
int | UNINITIALIZED |
The constant 0 for XMLHTTPRequest.readyState. More... | |
int | VERIFYHOST |
Constant for CA Certificate verification if using HTTPS. More... | |
int | VERIFYPEER |
Constant for CA Certificate verification if using HTTPS. More... | |
The XMLHTTPRequest class represents a HTTP request.
Though the name of this class traditionally refers to XML, it can be used to transfer arbitrary strings or binary data. The interface is based on the definition of the class IXMLHTTPRequest
from MSXML. As http-library the libcurl is used. To send a HTTP request the following steps are needed:
See SOAP Client using DOM for a detailed example using XMLHTTPRequest.
useProxySetting
= false
XMLHTTPRequest XMLHTTPRequest::XMLHTTPRequest | ( | String | proxy = "" , |
int | proxyPort = 3128 , |
||
String | proxyUser = "" , |
||
String | proxyPasswd = "" |
||
) |
Create a new XMLHTTPRequest object.
proxy | Optional string value specifying the hostname of the proxy server being resolvable by the nameserver. On windows OS: If this parameter is not specified, the windows proxy configuration will be used. E.g. the proxy server specified in Internet Explorer is used in the registry. |
proxyPort | Optional number of the port on which the proxy accepts requests. |
proxyUser | Optional string value specifying the desired login name for the proxy |
proxyPasswd | Optional string value specifying the password for logging in to the proxy |
XMLHTTPRequest XMLHTTPRequest::XMLHTTPRequest | ( | bool | useProxySetting = true | ) |
Create a new XMLHTTPRequest object with option to switch off the query mechanism for proxy settings.
useProxySetting | Boolean value. Set to false to switch off the query for proxy settings. |
boolean XMLHTTPRequest::abort | ( | ) |
Abort an asynchronous request if it already has been sent.
true
if successful, false
in case of any error. boolean XMLHTTPRequest::addRequestHeader | ( | String | name, |
String | value | ||
) |
Add a HTTP header to the request to be sent.
name | String specifying the header name. |
value | String specifying the header value. |
true
if the header was added successfully, false
in case of any error. String XMLHTTPRequest::getAllResponseHeaders | ( | ) |
Get all response headers as a string.
Each entry is in a separate line and has the form 'name:value'.
String XMLHTTPRequest::getResponseHeader | ( | String | name | ) |
Get the value of the specified response header.
name | String specifying the response header name. |
boolean XMLHTTPRequest::open | ( | String | method, |
String | url, | ||
boolean | async = false , |
||
String | user = "" , |
||
String | passwd = "" |
||
) |
Initialize a HTTP request.
method | String specifying the used HTTP method. The following methods are available:
|
url | String containing the URL for this request. |
async | Optional flag indicating whether to handle the request asynchronously. In this case the operation send() returns immediately, in other word, it will not be waiting until a response is received. Asynchronous sending is only possible, when XMLHTTPRequest.canAsync returns true . If asynchronous sending is not possible, this flag will be ignored. For an asynchronous request you can use XMLHTTPRequest.readyState to get the current state of the request. |
user | Optional user name must be specified only if the HTTP server requires authentication. |
passwd | Optional password must also be specified only if the HTTP server requires authentication. |
true
if the request was successfully initialized, false
in case of any error. boolean XMLHTTPRequest::send | ( | String | content = "" | ) |
Send the request to the HTTP server.
The request must be prepared via open()
before.
content | Optional content to be sent; usually a String. See the remarks section about using other types. |
true
if the request was successfully performed or initiated (for asynchronous requests), false
in case of any error.
About files
Passed files must be opened in binary read mode. If the file is not rewindable (a named pipe, for instance), the property FileSizeHint should be set before sending. The property is useful to supress an automatic length scan. The function implicitly closes the File object, though the file may remain open for asynchronous operation. When an asynchronous request is completed, its associated files become closed outside the JavaScript environment.
About Arraybuffers
Passing a TypedArray (UInt8Array, Int16Array etc.) instead of an ArrayBuffer is possible, but not recommended. The actual implementation always sends the complete associated buffer. The result can be unexpected, if the TypedArray covers only a section of a bigger buffer. This behaviour might change in future releases.
void XMLHTTPRequest::setCAInfo | ( | String | pemFile, |
int | options | ||
) |
Set one or more certificates to verify the peer if using HTTPS.
pemFile | String with the path to pem-file with the servers certificates (bundle of X.509 certificates in PEM format). |
options | Integer with a bitmask of verification-options (XMLHTTPRequest.VERIFYPEER, XMLHTTPRequest.VERIFYHOST) |
boolean XMLHTTPRequest::canAsync |
Flag indicating whether asynchronous requests are possible on the used plattform.
boolean XMLHTTPRequest::canProxy |
Flag indicating whether the implementation on the used plattform allows the direct specifying of a proxy server.
int XMLHTTPRequest::COMPLETED |
The constant 4 for XMLHTTPRequest.readyState.
In this state the request is completed. All the data are available now.
int XMLHTTPRequest::FileSizeHint |
Optional File size indicator for sending pure sequential files.
When uploading files, the send() function usually detects the file size and forwards it to lower APIs. This is helpful in most cases, because old simple HTTP servers do not support the transfer mode "chunked". Web services may reject uploads without an announced content-length, too.
However, the auto-detection will fail, if a given file is not rewindable (a named pipe, for instance). To avoid errors this property should be set before sending such a special file. After the transmission the property should be either set to "-1" or deleted.
The value is interpreted in the following way.
int XMLHTTPRequest::INTERACTIVE |
The constant 3 for XMLHTTPRequest.readyState.
In this state the request is partially completed. This means that some data has been received.
int XMLHTTPRequest::NOTSENT |
The constant 1 for XMLHTTPRequest.readyState.
In this state the object has been initialized, but not sent yet.
int XMLHTTPRequest::readyState |
The current state of the asynchronous request.
The following states are available:
Var XMLHTTPRequest::response |
The response received from the HTTP server or null if no response is received.
responseType
. The default type is String
. For requests with an attached responseFile
this value can be truncated after a few kBytes. File XMLHTTPRequest::responseFile |
An optional writable file for streaming a large response.
To achieve an efficient download scripts can create a writable File
an attach it to the request. The complete response will then be written into this file. The value of the response
property, however, will be truncated after the first few kBytes.
null
. Even in asynchronous mode send
() seems to close the file immediately. In fact, send
() detaches the native file handle from the JavaScript object to ensure exclusive access. String XMLHTTPRequest::responseType |
Preferred output format of the response property (optional).
By default, the object expects text responses and stores them in a String. If the application expects binary data, it may request an ArrayBuffer by setting this property to "arraybuffer".
int XMLHTTPRequest::SENT |
The constant 2 for XMLHTTPRequest.readyState.
In this state the object has been sent. No data is available yet.
int XMLHTTPRequest::status |
The HTTP status code of the request.
String XMLHTTPRequest::statusText |
The HTTP status text of the request.
int XMLHTTPRequest::UNINITIALIZED |
The constant 0 for XMLHTTPRequest.readyState.
In this state the method open() has not been called.
int XMLHTTPRequest::VERIFYHOST |
Constant for CA Certificate verification if using HTTPS.
This option activate the verification of the host.
int XMLHTTPRequest::VERIFYPEER |
Constant for CA Certificate verification if using HTTPS.
This option activate the verification of the certificate chain.