This method sends an HTTP POST request to the server.
The following example shows the memoryStreamObj being posted to http://search.aol.com. The listener which will
receive the response callback, and a context value which will be returned in the onResponse or onError callback.
var memoryStreamObj;
var contextData = 1;
function main()
{
var httpRequest = shell.serviceManager.http.request;
var listener = new httpListener();
var context = new contextObject(contextData);
memoryStreamObj = shell.serviceManager.basics.memoryStream;
var streamWriterObj = shell.serviceManager.basics.rawStreamWriter;
memoryStreamObj.size = 1024;
streamWriterObj.stream = memoryStreamObj;
streamWriterObj.writeString("data to POST to the server", "");
httpRequest.post("http://search.aol.com", memoryStreamObj, listener, context);
}
function contextObject(num)
{
this.num = num;
}
function httpListener()
{
this.onResponse = onResponse;
this.onError = onError;
}
function onResponse(requestor, code, text, header, payload, context)
{
alert("The request returned code: " + code);
alert("The status text for the return is: " + text);
// the context value will match the 1 specified in the get
alert("The context of this request is: " + context.num);
// output the payload, to show the html for aol.com
if (payload)
{
var streamReader = shell.serviceManager.basics.rawStreamReader;
streamReader.stream = payload;
var text = streamReader.readStringTillEndOfStream("");
alert("The payload returned is: " + text);
}
// output a field from the returned header
if (header)
{
// ** Note ** if Content-Length field does not exist, the following code will throw a JavaScript exception.
var headerFieldArray = header.valueForKey("Content-Length");
// use a 0 to get the first value, because the Content-Length header field only contains one value.
var contentLength = headerFieldArray.getValue(0);
if (contentLength)
{
alert("Content Length returned is: " + contentLength);
}
}
if (memoryStreamObj)
{
memoryStreamObj.close();
}
}
function onError(requestor, code, context)
{
alert("Http error: " + code);
if (memoryStreamObj)
{
memoryStreamObj.close();
}
}