Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************************
- * Leverages the Apache HTTPClient library to perform simple http
- * and https GET operations from OScript.
- *
- * Note: The Apahce library supports many more features than this
- * simple HTTP GET style operations. These include BASIC,
- * Digest and form based authentication, performing POST
- * operations, etc. I only needed the ability to do a GET
- * so I implmeneted it driectly in OScript Here. If you want
- * to do more complex operations, I would strongly suggest
- * implementing it in Java and then using JavaObject to
- * leverage your Java class library.
- *
- * Prerequisites:
- *
- * 1. You have downloaded the Apahce HTTPClient library from:
- * http://hc.apache.org/httpcomponents-client-ga
- * 2. You have added the seven jar files to Livelink's classpath
- * by (a) dropping them in an 'ojlib' folder of your module
- * or (b) dropping them in [OTHOME]/ojlib
- * 3. If you want Java commands to work when running builder,
- * don't forget to add '-loadjavavm' to your Builder shortcut.
- * (right click on the Builder shortcut, properties, target)
- *
- * Example Usage:
- *
- * String url = "http://www.google.com"
- * Assoc retVal = $aaamod.Utils.HttpGet(url)
- * if (retVal.OK)
- * echo(retVal.Data)
- * else
- * echo(Str.Format("Error fetching %1! %2.", url, retVal.ErrMsg)
- * end
- * url = "https://www.google.com"
- * retVal = $aaamod.Utils.HttpGet(url)
- * if (retVal.OK)
- * echo(retVal.Data)
- * else
- * echo(Str.Format("Error fetching %1! %2.", url, retVal.ErrMsg)
- * end
- *
- * Input Parameters:
- *
- * url - The URL you want to GET
- *
- * Output Parameters:
- *
- * retVal.Ok - Indiacates whether the operation worked
- * retVal.ErrMsg - Error message for failed operations.
- * retVal.ApiError - Actual error object if available.
- * retVal.Url - Url fetched
- * retVal.Data - Content of the web page as a String.
- *
- ********************************************************************/
- function Assoc HTTPGet(String url = "https://www.google.com")
- Assoc retVal
- retVal.Ok = false
- retVal.ErrMsg = Undefined
- retVal.ApiError = Undefined
- retVal.Data = Undefined
- Dynamic jo = JavaObject
- JavaObject request, response, content
- String data
- //build request object
- request = jo.InvokeStaticMethod("org.apache.http.client.fluent.Request","Get", { url })
- if (IsError(request))
- retVal.ErrMsg = Str.Format("%1", request)
- retVal.ApiError = request
- else
- //build response object from request
- response = request.InvokeMethod("execute", {})
- if (IsError(response))
- retVal.ErrMsg = Str.Format("%1", response)
- retVal.ApiError = response
- else
- //get content object from response
- content = response.InvokeMethod("returnContent", {})
- if (IsError(content))
- retVal.ErrMsg = Str.Format("%1", content)
- retVal.ApiError = content
- else
- //read text of web page into data
- data = content.InvokeMethod("asString", {})
- if (IsError(data))
- retVal.ErrMsg = Str.Format("%1", content)
- retVal.ApiError = content
- else
- retVal.Ok = true
- retVal.Url = url
- retVal.Data = data
- end
- end
- end
- end
- if (!retVal.Ok)
- if (IsUndefined(retVal.ErrMsg))
- retVal.ErrMsg = "Unexpected error occurred in HTTPGet"
- end
- end
- return retVal
- end
Advertisement
Add Comment
Please, Sign In to add comment