如何在Flash中捕获HTTP 404

时间:2010-04-16 14:32:22

标签: flash actionscript-3 actionscript

当我使用错误的URL(在URL末尾添加数字'1')执行下面的代码(第二个)时,我得到以下错误。如果网址错误,我该如何捕获此错误,以便我可以向用户发出错误消息?

打开URL时出错'http://localhost/myapp/cgi-bin/savePlanScale.ashx1?NoCache%5FRaumplaner = F7CF6A1E%2D7700%2D8E33%2D4B18%2D004114DEB39F& ScaleString = 5%2E3& ModifyFile = data%2Fdata%5Fzimmere03e1e83%2D94aa% 2D488b%2D9323%2Dd4c2e8195571%2Exml” httpStatusHandler:[HTTPStatusEvent type =“httpStatus”bubbles = false cancelable = false eventPhase = 2 status = 404] 状态:404 错误:错误#2101:Der a URLVariables.decode()übergebeneStringmuss ein URL-kodierter Abfrage-String mit Name / Wert-Paaren sein。  at Error $ / throwError()  在flash.net::URLVariables/decode()  在flash.net::URLVariables()  在flash.net::URLLoader/onComplete()


    public static function NotifyASPXofNewScale(nScale:Number)
{
   var strURL:String ="http://localhost/myapp/cgi-bin/savePlanScale.ashx1"
   // CAUTION: when called from website, RELATIVE url...
    var scriptRequest:URLRequest = new URLRequest(strURL);
   var scriptLoader:URLLoader = new URLLoader();
   // loader.dataFormat = URLLoaderDataFormat.TEXT; // default, returns as string
   scriptLoader.dataFormat = URLLoaderDataFormat.VARIABLES; // returns URL variables
   // loader.dataFormat = URLLoaderDataFormat.BINARY; // to load in images, xml files, and swf instead of the normal methods
   var scriptVars:URLVariables = new URLVariables();

   scriptLoader.addEventListener(Event.COMPLETE, onLoadSuccessful);
   scriptLoader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
   scriptLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
   scriptLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);

   scriptVars.NoCache_Raumplaner = cGUID.create();
   scriptVars.ScaleString = nScale;
   scriptVars.ModifyFile = "data/data_zimmere03e1e83-94aa-488b-9323-d4c2e8195571.xml";

   scriptRequest.method = URLRequestMethod.GET;
   scriptRequest.data = scriptVars;

   scriptLoader.load(scriptRequest);


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

   function onLoadSuccessful(evt:Event):void
   {
        trace("cSaveData.NotifyASPXofNewScale.onLoadSuccessful");
        trace("Response: " + evt.target.data);


        ExternalInterface.call("alert", "Die neue Skalierung wurde erfolgreich gespeichert.");
        //getURL("javascript:alert(\""+"Die neue Skalierung wurde erfolgreich gespeichert.\\nALLE Instanzen des Browsers schliessen und neu starten, damit die Änderung in Kraft tritt."+"\");");

        if (evt.target.data.responseStatus == "YOUR FAULT")
        {
            trace("Error: Flash transmitted an illegal scale value.");
            ExternalInterface.call("alert", "Fehler: Flash konnte die neue Skalierung nicht abspeichern.");
        }

        if (evt.target.data.responseStatus == "EXCEPTION")
        {
            trace("Exception in ASP.NET: " + evt.target.data.strError);
            ExternalInterface.call("alert", "Exception in ASP.NET: " + evt.target.data.strError);
        }
       }


       function onLoadError(evt:IOErrorEvent):void
       {
       trace("cSaveData.NotifyASPXofNewScale.onLoadError");
       trace("Error: ASPX or Transmission error. ASPX responseStatus: " + evt);
       ExternalInterface.call("alert", "ASPX - oder Übertragungsfehler.\\nASPX responseStatus: " + evt);
       //getURL("javascript:alert(\"" + "ASPX - oder Übertragungsfehler.\\nASPX responseStatus: " + receiveVars.responseStatus + "\");");
   }


   function onSecurityError(evt:SecurityErrorEvent):void
   {
        trace("cSaveData.NotifyASPXofNewScale.onSecurityError");
        trace("Security error: " + evt);
        ExternalInterface.call("alert", "Sicherheitsfehler. Beschreibung: " + evt);
   }

   }

1 个答案:

答案 0 :(得分:1)

来自http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/URLLoader.html#event:httpStatus

  

httpStatus:如果对URLLoader.load()的调用尝试,则调度   通过HTTP访问数据。对于在Flash Player中运行的内容,此事件   仅在当前Flash Player环境能够执行时才调度   检测并返回请求的状态代码。 (有些浏览器   环境可能无法提供此信息。)请注意   httpStatus事件(如果有的话)在任何之前发送(以及除此之外)   完成或错误事件。

您是否正在运行Flash Player调试器?您是否尝试过阅读从浏览器打开应用程序而产生的跟踪消息,而不仅仅是播放器?从上面的文字看,它似乎只会在某些情况下发射。