BinaryHTTPService or how to help an HTTPService accept ByteArray data

    During the development process, I had the task of finding a way to receive binary data from the server via HTTPService. The HTTPService in the mx package alone does not allow getting the result in ByteArray. This can be seen in the mx.messaging.channels.DirectHTTPChannel class in the internalSend function.

    1.  
    2. package mx.messaging.channels
    3. {
    4.   publicclass DirectHTTPChannel extends Channel
    5.   {
    6.  
    7.     .....
    8.  
    9.     override protectedfunction internalSend(msgResp:MessageResponder):void
    10.     {
    11.         var httpMsgResp:DirectHTTPMessageResponder = DirectHTTPMessageResponder(msgResp);
    12.         var urlRequest:URLRequest;
    13.  
    14.         try
    15.         {
    16.             urlRequest = createURLRequest(httpMsgResp.message);
    17.         }
    18.         catch(e: MessageSerializationError)
    19.         {
    20.             httpMsgResp.agent.fault(e.fault, httpMsgResp.message);
    21.             return;
    22.         }
    23.  
    24.         var urlLoader:URLLoader = httpMsgResp.urlLoader;
    25.         urlLoader.addEventListener(ErrorEvent.ERROR, httpMsgResp.errorHandler);
    26.         urlLoader.addEventListener(IOErrorEvent.IO_ERROR, httpMsgResp.errorHandler);
    27.         urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, httpMsgResp.securityErrorHandler);
    28.         urlLoader.addEventListener(Event.COMPLETE, httpMsgResp.completeHandler);
    29.         urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpMsgResp.httpStatusHandler);
    30.         urlLoader.load(urlRequest);
    31.     }
    32.    
    33.     .....
    34.    
    35.   }
    36. }
    37.  

    For URLLoader, by default, the dataFormat parameter is set to URLLoaderDataFormat.TEXT, and to receive data in the form of ByteArray, you need URLLoaderDataFormat.BINARY.

    Everything is done very simply. The first thing we need is to create an extension for the DirectHTTPChannel class and set the urlLoader parameter dataFormat = URLLoaderDataFormat.BINARY;
    1.  
    2. publicclass DirectHTTPBinaryChannel extends DirectHTTPChannel
    3. {
    4.     publicfunction DirectHTTPBinaryChannel(id:String, uri:String="")
    5.     {
    6.         super(id, uri);
    7.     }
    8.  
    9.     override protectedfunction getMessageResponder(agent:MessageAgent,
    10.                                                     message:IMessage):MessageResponder
    11.     {
    12.         returnnew DirectHTTPBinaryMessageResponder(agent, message, this, new URLLoader());
    13.     }
    14.  
    15.     override protectedfunction internalSend(msgResp:MessageResponder):void
    16.     {
    17.         var httpMsgResp:DirectHTTPBinaryMessageResponder = DirectHTTPBinaryMessageResponder(msgResp);
    18.         var urlRequest:URLRequest;
    19.  
    20.         try
    21.         {
    22.             urlRequest = createURLRequest(httpMsgResp.message);
    23.         }
    24.         catch(e: MessageSerializationError)
    25.         {
    26.             httpMsgResp.agent.fault(e.fault, httpMsgResp.message);
    27.             return;
    28.         }
    29.  
    30.         var urlLoader:URLLoader = httpMsgResp.urlLoader;
    31.         urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
    32.         urlLoader.addEventListener(ErrorEvent.ERROR, httpMsgResp.errorHandler);
    33.         urlLoader.addEventListener(IOErrorEvent.IO_ERROR, httpMsgResp.errorHandler);
    34.         urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, httpMsgResp.securityErrorHandler);
    35.         urlLoader.addEventListener(Event.COMPLETE, httpMsgResp.completeHandler);
    36.         urlLoader.load(urlRequest);
    37.     }
    38. }
    39.  

    So that we can use our DirectHTTPBinaryChannel, we create an extension of the HTTPService class.
    1.  
    2. publicclass BinaryHTTPService extends HTTPService
    3. {
    4.   protected static var binaryChannel:Channel;
    5.   protected static var binaryChannelSet:ChannelSet;
    6.  
    7.   publicfunction BinaryHTTPService(rootURL:String = null, destination:String = null)
    8.   {
    9.     super(rootURL, destination);
    10.   }
    11.  
    12.   override publicfunction send(parameters:Object = null):AsyncToken
    13.   {
    14.     if (useProxy == false)
    15.     {
    16.       /* force the use of our binary channel */
    17.       if (binaryChannelSet == null)
    18.       {
    19.         var dcs:ChannelSet = new ChannelSet();
    20.         binaryChannel = new DirectHTTPBinaryChannel("direct_http_binary_channel");
    21.         dcs.addChannel(binaryChannel);
    22.         channelSet = dcs;
    23.         binaryChannelSet = dcs;
    24.       }
    25.       elseif (channelSet != binaryChannelSet)
    26.       {
    27.         channelSet = binaryChannelSet;
    28.       }
    29.     }
    30.     returnsuper.send(parameters);
    31.   }
    32. }
    33.  

    Work Class Example: BinaryHTTPService DirectHTTPBinaryChannel

    Also popular now: