如何在 HTTPService 故障处理程序中获取 HTTP 状态代码

发布于 2024-08-28 00:02:54 字数 818 浏览 7 评论 0原文

我从客户端通过 HTTPService 调用服务器方法。服务器是一个 RestFul Web 服务,它可能会使用许多 HTTP 错误代码之一进行响应(例如,一个错误为 400,另一个错误为 404,另一个错误为 409)。我一直在尝试找出确定服务器发送的确切错误代码的方法。我已经遍历了故障处理程序中填充的FaultEvent 的整个对象树,但它没有告诉我错误代码。 Flex 中缺少此功能吗?

我的代码如下所示: HTTP 服务声明:

    <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>

我的错误处理程序代码如下所示:

private function faultHandler(event : FaultEvent):void
{
 Alert.show(event.statusCode.toString() + " / " + event.fault.message.toString()); 
}

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(event : FaultEvent):void
{
 Alert.show(event.statusCode.toString() + " / " + event.fault.message.toString()); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

影子是时光的心 2024-09-04 00:02:55

我可能在这里遗漏了一些东西,但是:

event.statusCode

为我提供了 HTTP 响应的状态代码。

所以我可以在我的故障处理函数中成功地执行类似的操作:

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);
    }
}

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);
    }
}
素罗衫 2024-09-04 00:02:55

看起来你运气不好: http ://fantastic.wordpress.com/2007/12/26/flex-is-not-friend-to-rest/

您可能必须使用ExternalInterface 在JS 中处理此问题,然后与Flex 进行通信。

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.

冰之心 2024-09-04 00:02:55

Flash Player 需要浏览器的帮助才能访问 HTTP 状态代码;因此,这并非在所有平台上都可用。对我来说,它在 Flash Player 10.3.183.11 和 Firefox 3.6.26 上失败,但在 Windows 7 上的 IE 8 上运行正常。

FaultEvent.statusCode 属性的 Adob​​e 帮助对此进行了暗示,但不幸的是没有。不详述:

此属性提供对 HTTP 响应状态代码(如果可用)的访问,否则值为 0

因此,如果您绝对需要状态代码,那么运气不好;如果只是为了针对某些频繁的错误情况生成更好或更友好的错误消息,可能就足够了。

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.

梦醒时光 2024-09-04 00:02:55

Ross 发布的 as3httpclient 对 Rest 很友好,并为您提供 HTTP 状态代码,只要您正在为 AIR 开发,而不是基于浏览器的应用程序。

即使向同一来源发出请求,我也无法让 as3httpclient 从浏览器工作。有文档指出您需要设置一个套接字策略文件服务器才能获得这个工作。对于我们的使用来说无法扩展,因此我在运行 Flex 应用程序的同一主机上设置了代理 Web 服务。

我使用 HTTPService 调用代理 Web 服务,代理 Web 服务将请求转发到目的地,代理 Web 服务将 http 状态代码和消息正文以 xml 形式返回给 HTTPService。

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.

疑心病 2024-09-04 00:02:55

尝试使用它代替 HTTPService:
http://code.google.com/p/as3httpclient/

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文