Posted By

bcmoney on 12/10/10


Tagged

http actionscript flash as3 proxy


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

thastyle


proxy.as


 / Published in: ActionScript 3
 

URL: http://bcmoney-mobiletv.com/blog/2009/05/01/the-server-side-proxy/

ActionScript 3.0 for making a cross-domain HTTP request (which also requires use of a "crossdomain.xml" Flash security policy file)

crossdomain.xml resources: http://www.adobe.com/devnet/articles/crossdomainpolicyfile_spec.html

http://curtismorley.com/2007/09/01/flash-flex-tutorial-how-to-create-a-crossdomainxml-file/

http://stackoverflow.com/questions/101427/flex-and-crossdomain-xml

Flash/Flex to AJAX Bridge: http://www.ibm.com/developerworks/web/library/wa-aj-flex/

FlashXMLHttpRequest -- cross-domain requests: http://blog.monstuff.com/archives/000294.html

Cross-domain AJAX using Flash: http://blog.monstuff.com/archives/000280.html

flXHR -- VERY complete library for bridging Flash/AJAX and making cross-domain requests: http://flxhr.flensed.com

Other resources: http://www.securiteam.com/securityreviews/5KP0M1FJ5E.html

http://www.abdulqabiz.com/blog/archives/2006/03/03/http-authentication-for-httpget-requests-using-actionscript-3/

http://snipplr.com/view/5124/as3-sending-and-recieving-data-using-a-get-request/

http://stackoverflow.com/questions/702880/how-to-change-javascript-function-to-actionscript-3

http://www.switchonthecode.com/tutorials/using-a-php-proxy-with-flex-to-talk-cross-domain

  1. package {
  2. import flash.display.*;
  3. import flash.events.*;
  4. import flash.utils.ByteArray;
  5.  
  6. public class Proxy extends Sprite {
  7. [Embed(source="embeds/data.xml", mimeType="application/octet-stream")]
  8. private var BinaryData:Class;
  9.  
  10.  
  11. public function Proxy () {
  12. var byteArray:ByteArray = new BinaryData(); // Create a new instance of the embedded data
  13. var data:XML = new XML(byteArray.readUTFBytes(byteArray.length)); // Convert the data instance to XML
  14. trace(data.toXMLString()); // Display the source code for the embedded XML
  15. }
  16.  
  17. var requestVars:URLVariables = new URLVariables();
  18. requestVars.object_name = "ts";
  19. requestVars.cache = new Date().getTime(); //prevent caching by using a timestamp
  20.  
  21. var request:URLRequest = new URLRequest();
  22. request.url = getParams("url"); //get the "url" parameter to set the HTTP request URL (path to data source)
  23. request.contentType = getParams("f"); //get the "f" parameter to set the MIME-type (format)
  24. request.type = getParams("e"); //get the "e" parameter to set the encoding type (i.e. UTF-8)
  25. request.method = URLRequestMethod.GET;
  26. request.data = requestVars;
  27.  
  28. for (var prop:String in requestVars) {
  29. trace("Sent " + prop + " as: " + requestVars[prop]);
  30. }
  31.  
  32. var loader:URLLoader = new URLLoader();
  33. loader.dataFormat = URLLoaderDataFormat.TEXT;
  34. loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
  35. loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
  36. loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  37. loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  38.  
  39. try
  40. {
  41. loader.load(request);
  42. }
  43. catch (error:Error)
  44. {
  45. trace("Unable to load URL");
  46. }
  47.  
  48. public function loaderCompleteHandler(e:Event):void {
  49. var variables:URLVariables = new URLVariables( e.target.data );
  50. if(variables.success) {
  51. trace(variables.path);
  52. }
  53. }
  54.  
  55. private function httpStatusHandler (e:Event):void {
  56. trace("httpStatusHandler:" + e);
  57. }
  58.  
  59. private function securityErrorHandler (e:Event):void {
  60. trace("securityErrorHandler:" + e);
  61. }
  62.  
  63. private function ioErrorHandler(e:Event):void {
  64. trace("ioErrorHandler: " + e);
  65. }
  66.  
  67. public function getParams(documentRoot):Object {
  68. try {
  69. var params:Object = LoaderInfo(documentRoot.loaderInfo).parameters;
  70. var pairs:Object = {};
  71. var key:String;
  72.  
  73. for(key in params) {
  74. pairs.key = String(params.key);
  75. }
  76. } catch(e:Error) {
  77. return {};
  78. }
  79. return params;
  80. }
  81. }

Report this snippet  

You need to login to post a comment.