开发者

How to get HTTP status code in HTTPService fault handler

开发者 https://www.devze.com 2022-12-26 00:12 出处:网络
I am calling a server method through HTTPService from client side. The server is a RestFul web service and it might respond with one of many HTTP error codes (say, 400 for one error, 404 for another a

I am calling a server method through HTTPService from client side. The server is a RestFul web service and it might respond with one of many HTTP error codes (say, 400 for one error, 404 for another and 409 for yet another). I have been trying to find out the way to determine what was the exact error code sent by the server. I have walked teh entire object tree for the FaultEvent populated in my fault handler, but no where does it tell me the error code. Is this missing functionality in Flex?

My code looks like this: The HTTP Service declaration:

    <mx:HTTPService id="myServerCall" url="myService" method="GET" 
resultFormat="e4x" result="myServerCallCallBack(event)" fault="faultHandler(event)">
            <mx:request>
                <action>myServerCall</action>
                <docId>{m_sDocId}</docId>
            </mx:request>
        </mx:HTTPService>

My fault handler code is like so:

private function faultHandler(even开发者_JAVA技巧t : FaultEvent):void
{
 Alert.show(event.statusCode.toString() + " / " + event.fault.message.toString()); 
}


I might be missing something here, but:

event.statusCode

gives me the status code of the HTTP response.

So I can successfully do something like this in my fault handler function:

public function handleFault(faultEvent:FaultEvent):void
{
    if (faultEvent.statusCode == 401)
    {
        Alert.show("Your session is no longer valid.", "", Alert.OK, this, loginFunc);
    }
    else
    {
        Alert.show("Failed with error code: " + faultEvent.statusCode as String);
    }
}


Looks like you are out of luck: http://fantastic.wordpress.com/2007/12/26/flex-is-not-friendly-to-rest/

You may have to use ExternalInterface to get this handled in JS and then communicated to Flex.


The Flash Player needs help from the browser to be able to access the HTTP status code; therefore, this is not available on all platforms. For me, it failed with Flash Player 10.3.183.11 and Firefox 3.6.26, but worked with IE 8 on Windows 7.

The Adobe help for the FaultEvent.statusCode property hints at this, but unfortunately doesn't go into details:

this property provides access to the HTTP response status code (if available), otherwise the value is 0

So, if you absolutely need the status code, bad luck; if it's just to generate a better or friendlier error message for some frequent error conditions, it may be sufficient.


as3httpclient as posted by Ross is friendly to Rest, and provides you with the HTTP status code, as long as you're developing for AIR and not a browser-based app.

I could not get as3httpclient to work from the browser, even when making requests to the same origin. There's documentation stating you need to set up a socket policy file server to get this to work. Not scalable for our uses so I setup a Proxy web service on the same host running the flex app.

I use HTTPService to make the call to the proxy web service, which forwards the request to the destination, and the proxy web service returns the http status code and message body back to the HTTPService in xml.


Try using this instead of HTTPService: http://code.google.com/p/as3httpclient/

0

精彩评论

暂无评论...
验证码 换一张
取 消