That gets much more difficult...
If I told you that it was almost so much trouble that it is hard to justify doing it, would you believe me?
If not, here is the necessary code. It makes use of the listener tag to directly communicate back and forth with the server and read the response.
If this gets completely mangled by the forum, contact me at jpeltzer@rossvideo.com and I'll send you the file.
`
if (event.isConnectEvent())
{
var host = params.getValue('Host', 0);
var encodedPassword = params.getValue('Username', 0);
var path = params.getValue('Path', 0);
var message = 'GET ' + path + ' HTTP/1.1rn' + //You will replace "/password-ok.php" with your desired path
'Host: ' + host + 'rn' + //replace with your destination host/IP
'Connection: closern' +
'Authorization: Basic ' + encodedPassword + 'rn' + //this is a base64 encoding of 'test:test' [Username]:[password] you can do the encoding at https://www.base64encode.org/
'rn'; //HTTP headers end with a blank line.
this.writeString(message, false);
ogscript.rename('content', 'SENDING: ' + message);
}
else if (event.isMessageEvent())
{
try //No matter what happens, we want to close our connection so we put it in a try/catch block
{
var header = event.getBytesAsString(); //Get the first line of the header (contains response code
var combinedHeader = header + 'n'; //Keep track of our combined header
ogscript.rename('content', 'HEADER: ' + header); //DEBUG CODE
if (header.indexOf('200') > 0) //If the response is OK
{
var messageParser = ogscript.createMessageParser(this.getInputStream()); //We are going to read directly from our connection
var chunked = false; //HTTP supports chunked or content-length encoding
var contentLength = 0; //We are going to read directly from our connection
header = messageParser.readLine().trim(); //Read the next line in the header
combinedHeader += header + 'n'; //Keeping track of the overall header for debugging
while (header.length > 0) //The header ends with an empty line so we keep reading to build the header
{
if (header.indexOf('Content-Length') == 0) //If we get the Content-Length field, we know how many bytes to read
{
contentLength = parseInt(header.substr(('Content-Length').length + 1).trim());
}
else if (header.indexOf('Transfer-Encoding: chunked') == 0) //If we get the CHUNKED field, we know we need to read in chunks
{
chunked = true;
}
header = messageParser.readLine().trim(); //Get the next line of the header
combinedHeader += header + 'n'; //Continue to build the header
ogscript.rename('content', 'HEADER: ' + header);//DEBUG CODE
}
if (contentLength > 0) //If we have a specified content length, just read that string
{
var stringValue = messageParser.readString(contentLength);
ogscript.rename('content', stringValue);
}
else if (chunked) //If we have a chunked encoding, we need to read each chunk
{
var stringValue = ''; //We're going to build our string
while (true) //We're going to keep reading until we run out of chunks
{
var chunkLengthStr = messageParser.readLine().trim(); //Get the next chunk length line
if (chunkLengthStr.length > 0)
{
var chunkLength = parseInt(chunkLengthStr, 16); //CHUNKS start with their length in hex
if (chunkLength == 0) //The final chunk
{
break;
}
else
{
stringValue += messageParser.readString(chunkLength); //Read the actual chunk
}
messageParser.readLine(); //skip the CRLF at the end of the chunk
}
}
ogscript.rename('content', stringValue); //We have a complete HTTP string! Yay!
}
else
{
ogscript.rename('content', combinedHeader); //Something unexpected happen, output the header so we can take a closer look
}
}
}
catch (e)
{
ogscript.rename('content', "EXCEPTION: " + e + ' ' + header);//DEBUG CODE
}
ogscript.getListenerById('http-sender').stop(); //We are done with the connection. Stop the listener.
}
var host = params.getValue('Host', 0);
var port = params.getValue('Port', 0);
ogscript.getListenerById('http-sender').connectTo(host, port);
`
#DashBoard