Flash and HTTP Status Code 207

2019-07-20 15:00发布

问题:

Having some trouble getting flash to recognize a valid HTTP response code (207). Oddly, this only seems to happen in FireFox so far.. works fine in Chrome.

Here's the code that's generating the errors below. It appears to return an HTTP status of 0 in FlashBug.. must be handled differently in Chrome? Is there any way to still get the response body?

Nothing I do seems to be able to get me the response body in this 207 condition. :(

    // Initiate a call to a Patron URL
    private function callPatron( url:String, callback:Function ) {
        trace("Calling Patron");
        _loader.addEventListener( Event.COMPLETE, function(e:Event) { callback( parseResponse(e) ) });
        _loader.addEventListener( IOErrorEvent.IO_ERROR, gotError );
        _loader.addEventListener( "httpResponseStatus", onStatus );
        _loader.addEventListener( ProgressEvent.PROGRESS, progressHandler );
        _loader.addEventListener( HTTPStatusEvent.HTTP_STATUS, onStatus );          
        _loader.load( new URLRequest( url ) );
    }

    private function progressHandler(event:ProgressEvent) {
        trace("Progresso");
    }

    private function onStatus(event:HTTPStatusEvent) {
        trace('Got HTTP status: ' + event.status );
        trace( event.toString() );
    }

    private function gotError(event:IOErrorEvent) {
        trace( 'IOError: ' + event.text );
        var loader:URLLoader = URLLoader( event.target );
        trace( loader.content );
        trace( 'WORD.' );
    }

    // Decode a Patron response event and return the parsed object
    public function parseResponse(event:Event):Object {
        trace("Got a Patron response");
        var loader:URLLoader = URLLoader( event.target );
        return JSON.decode( loader.data );
    }

..and the output:

Initializing
Calling Patron
Got HTTP status: 0
[HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=0 responseURL=null]
IOError: Error #2032: Stream Error. URL: http://www.zappos.com/api/Product?key=5f25a02d8015e05ba3874e0b45be0379fe8b3c21&styleId=["1377484","1312254","269758","1519409","1325534","1152368"]&includes=["description","productRating","styles","thumbnailImageUrl"]
WORD.

回答1:

Unfortunately, you won't get a full set of HTTP response codes in Flash, in all browsers. The Flash Player doesn't have its own HTTP handling (when run as a browser plugin) and instead uses the hosting browser for HTTP calls. And somewhere along the line between the browser and the plugin, info like some response codes and headers gets reduced.

This article by Arc90 (the company behind Readability) discusses the problem, it is a bit dated and their solution won't work in all cases, as I understand it, but it may give some insight on the problem: http://lab.arc90.com/2008/03/25/flex-as3-library-restservice/