Flex 3 Closing UrlLoader Exception

I am trying to simulate the "HEAD" method using UrlLoader; Essentially, I just want to check for a file without loading everything. I assumed that I was just using HttpStatusEvent, but the following code throws an exception (which I cannot include in the try / catch block) when starting in debug mode.

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()"> <mx:Script> <![CDATA[ private static const BIG_FILE:String = "http://www.archive.org/download/gspmovvideotestIMG0021mov/IMG_0021.mov"; private var _loader:URLLoader; private function init():void { _loader = new URLLoader(); _loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, statusHandler); _loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); _loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler); _loader.load(new URLRequest(BIG_FILE)); } public function unload():void { try { _loader.close(); _loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, statusHandler); _loader.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler); _loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler); } catch(error:Error) { status.text = error.message; } } private function errorHandler(event:Event):void { status.text = "error"; unload(); } private function statusHandler(event:HTTPStatusEvent):void { if(event.status.toString().match(/^2/)) { status.text = "success"; unload(); } else { errorHandler(event); } } ]]> </mx:Script> <mx:Label id="status" /> 

I tried using ProgressEvents instead, but it seems that some 404 pages return content, so the status event will correctly identify if the page exists.

Does anyone have any ideas?

+4
source share
2 answers

I think this is a mistake in the URLLoader class.

If you read the error message (at least the one I received, you did not insert yorrs!), You will see the following:

Error: error # 2029: this URLStream object does not have an open stream on flash.net::URLStream/readBytes () on flash.net::URLLoader/onComplete ()

This gives you some idea of ​​what is going on. The URLLoader class internally uses the URLStream object, which provides low-level access to loaded data.

The error message indicates that the onComplete handler is being called on the URLLoader. The file is large, but it is probably cached, so it loads pretty quickly. Now, if you add a listener for both progress and full events, you will see the order in which events are fired:

  • progress
  • status
  • full

docs confirm this:

Note that the httpStatus event (if any) is dispatched before (and, in addition, to) any complete event or error.

Now you can see that the problem is that you are calling close () from the state handler. This closes the stream. But apparently (and this is an error), the onComplete handler in the URLLoader class does not check if the stream is open. (From Actionscript there is no way to verify this, so you have to wrap the code in try / catch). You cannot read data from a closed stream, which is why it is blowing.

I see two ways to fix this:

1) Cancel the execution of the function that calls the close () method (unload method), so the call to close () is called after calling the onComplete URLLoader internal method.

That is, do the following:

 setTimeout(unload,1); 

instead of this:

 unload(); 

2) Use URLStream instead of URLLoader. The first option seems like a bit of a hacky workaround for me, so I would go with that last one in your situation. Using a URLStream means more work on your side, as a rule, but in this case you are actually not interested in reading any data, so it does not really matter. In addition, you will need to change two lines in the current code:

This:

 private var _loader:URLStream; 

And this one:

 _loader = new URLStream(); 

And you are all set.

+6
source

I ran into a similar problem.
I found that the problem was in this call:

 _loader.close(); 

The error occurred when I tried to close a file that I didn’t even open. Therefore, in the try clause, check if the file is open before trying to close it.

Mike

+1
source

Source: https://habr.com/ru/post/1311242/


All Articles