As3 When i use Loader,i always get httpstatus code 200 event though the httpstatus code in google chrome is 304

主宰稳场 提交于 2019-12-25 09:41:28

问题


In As3, I use

_loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHttpStatus);

to detect the httpstatus code of Loader. In the callback function the httpstatus code is 200 when the httpstatus code show in google chrome is exactly 304.


回答1:


In reality, that should be the same thing because these status are reponses to your request sent by the web server, but with Flash while the requested file is in the cache, you will get always 200 HTTP status because in this case the lazy Flash didn't even sends a request to the web server and give you a fake 200 HTTP status. With Air, the situation is a little bit different because It gives you 200 HTTP status for a real 200 and for 304 HTTP status ( Not Modified ) despite Air do a real request every time.

To verify all that we can use this simple ActionScript code :

var loader:URLLoader = new URLLoader();
    loader.addEventListener(
        HTTPStatusEvent.HTTP_STATUS, 
        function(e:HTTPStatusEvent):void {
            trace('http status : ' + e.status)
        } 
    )
    loader.load(new URLRequest('http://www.example.com'));

Flash will give you always the 200 HTTP status for a reachable content of course. You can try with an on line URL and then disconnect your machine from Internet, you will get always the same value : 200.

For Air, the first request it's a real 200, but the second, if the file is cachebal, you will get 200 but in reality Air receive 304 response. To verify that, you can use a local web server and you will find web server responses in it's logs as 200 for the first request and 304 for the second one, or you can use a capturing network traffic tool as Fiddler and you will see directly responses received in reality by your Air app.

I hope that's more clear now for you.




回答2:


Use a random number to generate "new" links and avoid the cache effect

    var loader:URLLoader = new URLLoader();
    var noCache:Number = Math.random()
    loader.addEventListener(
        HTTPStatusEvent.HTTP_STATUS, 
        function(e:HTTPStatusEvent):void {
            trace('http status : ' + e.status)
        } 
    )
    loader.load(new URLRequest('http://www.example.com?c='+noCache));


来源:https://stackoverflow.com/questions/27500135/as3-when-i-use-loader-i-always-get-httpstatus-code-200-event-though-the-httpstat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!