使用 Adob​​e Flex/AIR 中的 HTTPService 对象进行 HTTP 基本身份验证 [英] HTTP Basic Authentication with HTTPService Objects in Adobe Flex/AIR

查看:23
本文介绍了使用 Adob​​e Flex/AIR 中的 HTTPService 对象进行 HTTP 基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Adob​​e AIR 应用程序中请求需要基本授权标头的 HTTP 资源.我尝试手动将标头添加到请求中,以及使用 setRemoteCredentials() 方法来设置它们,但无济于事.

I'm trying to request a HTTP resource that requires basic authorization headers from within an Adobe AIR application. I've tried manually adding the headers to the request, as well as using the setRemoteCredentials() method to set them, to no avail.

代码如下:

<mx:Script>
    <![CDATA[
        import mx.rpc.events.ResultEvent;
        import mx.rpc.events.FaultEvent;

        private function authAndSend(service:HTTPService):void
        {
            service.setRemoteCredentials('someusername', 'somepassword');
            service.send();
        }

        private function resultHandler(event:ResultEvent):void
        {
            apiResult.text = event.result.toString();
        }

        private function resultFailed(event:FaultEvent):void
        {
            apiResult.text = event.fault.toString();
        }
    ]]>
</mx:Script>

<mx:HTTPService id="apiService"
    url="https://mywebservice.com/someFileThatRequiresBasicAuth.xml"
    resultFormat="text"
    result="resultHandler(event)"
    fault="resultFailed(event)" />

<mx:Button id="apiButton"
    label="Test API Command"
    click="authAndSend(apiService)" />

<mx:TextArea id="apiResult" />

但是,仍然会弹出一个标准的基本身份验证对话框,提示用户输入用户名和密码.我有一种感觉,我没有以正确的方式做这件事,但我能找到的所有信息(Flex 文档、博客、Google 等)要么没有用,要么太模糊而无法提供帮助.

However, a standard basic auth dialog box still pops up prompting the user for their username and password. I have a feeling I'm not doing this the right way, but all the info I could find (Flex docs, blogs, Google, etc.) either hasn't worked or was too vague to help.

任何黑魔法,哦 Flex 大师?谢谢.

Any black magic, oh Flex gurus? Thanks.

将 setRemoteCredentials() 更改为 setCredentials() 会产生以下 ActionScript 错误:

Changing setRemoteCredentials() to setCredentials() yields the following ActionScript error:

[MessagingError message='Authentication not supported on DirectHTTPChannel (no proxy).']

<小时>

问题解决了,经过 Adob​​e 的一些关注.有关完整说明,请参阅下面的帖子.此代码适用于任意长度的 HTTP 身份验证标头.


Problem solved, after some attention from Adobe. See the posts below for a full explanation. This code will work for HTTP Authentication headers of arbitrary length.

import mx.utils.Base64Encoder;
private function authAndSend(service:HTTPService):void
{
        var encoder:Base64Encoder = new Base64Encoder();
        encoder.insertNewLines = false; // see below for why you need to do this
        encoder.encode("someusername:somepassword");

        service.headers = {Authorization:"Basic " + encoder.toString()};                                                
        service.send();
}

推荐答案

终于得到了 Adob​​e 的一些关注,并得到了一个答案.长 HTTP 身份验证标头的问题在于,默认情况下 Base64Encoder 类将每 72 个字符注入换行符.显然,这会导致 base-64 编码字符串的一个块被解释为新的标头属性,从而导致错误.

Finally received some attention from Adobe and got an answer on this. The problem with long HTTP Authentication headers is that, by default, the Base64Encoder class will inject newline characters every 72 characters. Obviously that causes a chunk of the base-64 encoded string to be interpreted as a new header attribute, which causes the error.

您可以通过设置(在上面的示例中)encoder.insertNewLines = false; 默认设置为 true 来解决此问题.

You can fix this by setting (in the above example) encoder.insertNewLines = false; The default setting is true.

我已经修复了上面的代码以适用于任意长的身份验证字符串.

I've fixed the above code to work for arbitrarily long Authentication strings.

这篇关于使用 Adob​​e Flex/AIR 中的 HTTPService 对象进行 HTTP 基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆