URLRequest in AS3 can't read the correct content when server return 403

190 Views Asked by At

I'm using URLRequest in a Flash client to get some information from a service, the service will check the parameter in the request url, if the parameter is invalid, the server may return 403 with some content in http body, and I need analyse the content, the content is a json string:

{"code":3, "msg":"invalid channel"}

Unfortunately, sometimes the URLRequest can't read the content from URLStream, stream.bytesAvailable is 0, seems that the URLStream read null data. Is there any bug in URLRequest? Or it just because I'm using it in the wrong way?

Here's the code:

    public function test() {
        stream = new URLStream();
        request = new URLRequest(url);
        configureListeners(stream);
        var timer:Timer = new Timer(2000, 10);
        timer.addEventListener(TimerEvent.TIMER, onTimer);
        timer.start();
    }

    private function onTimer(e:TimerEvent):void {
        request.url = url + "&timestamp=" + getTimer();
        trace("try to load: " + request.url);
        stream.load(request);
    }

    private function configureListeners(dispatcher:EventDispatcher):void {
        dispatcher.addEventListener(Event.COMPLETE, completeHandler);
        dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
        dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    }

    private function completeHandler(event:Event):void {
        trace("completeHandler: " + event);
        parseHeader();
    }

    private function httpStatusHandler(event:HTTPStatusEvent):void {
        trace("httpStatusHandler: " + event);
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        trace("ioErrorHandler: " + event.toString());
        trace(stream.bytesAvailable + ", " + event.target.bytesAvailable + ", " + event.target.length);
    }

Here's the log: enter image description here

0

There are 0 best solutions below