ossiejhmoore

OScript HttpGet Leveraging Apache HttpClient

Aug 18th, 2012
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. /********************************************************************
  2. * Leverages the Apache HTTPClient library to perform simple http
  3. * and https GET operations from OScript.
  4. *
  5. * Note: The Apahce library supports many more features than this
  6. * simple HTTP GET style operations. These include BASIC,
  7. * Digest and form based authentication, performing POST
  8. * operations, etc. I only needed the ability to do a GET
  9. * so I implmeneted it driectly in OScript Here. If you want
  10. * to do more complex operations, I would strongly suggest
  11. * implementing it in Java and then using JavaObject to
  12. * leverage your Java class library.
  13. *
  14. * Prerequisites:
  15. *
  16. * 1. You have downloaded the Apahce HTTPClient library from:
  17. * http://hc.apache.org/httpcomponents-client-ga
  18. * 2. You have added the seven jar files to Livelink's classpath
  19. * by (a) dropping them in an 'ojlib' folder of your module
  20. * or (b) dropping them in [OTHOME]/ojlib
  21. * 3. If you want Java commands to work when running builder,
  22. * don't forget to add '-loadjavavm' to your Builder shortcut.
  23. * (right click on the Builder shortcut, properties, target)
  24. *
  25. * Example Usage:
  26. *
  27. * String url = "http://www.google.com"
  28. * Assoc retVal = $aaamod.Utils.HttpGet(url)
  29. * if (retVal.OK)
  30. * echo(retVal.Data)
  31. * else
  32. * echo(Str.Format("Error fetching %1! %2.", url, retVal.ErrMsg)
  33. * end
  34. * url = "https://www.google.com"
  35. * retVal = $aaamod.Utils.HttpGet(url)
  36. * if (retVal.OK)
  37. * echo(retVal.Data)
  38. * else
  39. * echo(Str.Format("Error fetching %1! %2.", url, retVal.ErrMsg)
  40. * end
  41. *
  42. * Input Parameters:
  43. *
  44. * url - The URL you want to GET
  45. *
  46. * Output Parameters:
  47. *
  48. * retVal.Ok - Indiacates whether the operation worked
  49. * retVal.ErrMsg - Error message for failed operations.
  50. * retVal.ApiError - Actual error object if available.
  51. * retVal.Url - Url fetched
  52. * retVal.Data - Content of the web page as a String.
  53. *
  54. ********************************************************************/
  55. function Assoc HTTPGet(String url = "https://www.google.com")
  56.  
  57. Assoc retVal
  58. retVal.Ok = false
  59. retVal.ErrMsg = Undefined
  60. retVal.ApiError = Undefined
  61. retVal.Data = Undefined
  62.  
  63. Dynamic jo = JavaObject
  64. JavaObject request, response, content
  65. String data
  66.  
  67. //build request object
  68. request = jo.InvokeStaticMethod("org.apache.http.client.fluent.Request","Get", { url })
  69. if (IsError(request))
  70. retVal.ErrMsg = Str.Format("%1", request)
  71. retVal.ApiError = request
  72. else
  73.  
  74. //build response object from request
  75. response = request.InvokeMethod("execute", {})
  76. if (IsError(response))
  77. retVal.ErrMsg = Str.Format("%1", response)
  78. retVal.ApiError = response
  79. else
  80.  
  81. //get content object from response
  82. content = response.InvokeMethod("returnContent", {})
  83. if (IsError(content))
  84. retVal.ErrMsg = Str.Format("%1", content)
  85. retVal.ApiError = content
  86. else
  87.  
  88. //read text of web page into data
  89. data = content.InvokeMethod("asString", {})
  90. if (IsError(data))
  91. retVal.ErrMsg = Str.Format("%1", content)
  92. retVal.ApiError = content
  93. else
  94. retVal.Ok = true
  95. retVal.Url = url
  96. retVal.Data = data
  97. end
  98. end
  99.  
  100. end
  101. end
  102.  
  103. if (!retVal.Ok)
  104. if (IsUndefined(retVal.ErrMsg))
  105. retVal.ErrMsg = "Unexpected error occurred in HTTPGet"
  106. end
  107. end
  108.  
  109. return retVal
  110.  
  111. end
Advertisement
Add Comment
Please, Sign In to add comment