HTTP基本认证使用的HTTPService在Adobe Flex对象/ AIR [英] HTTP Basic Authentication with HTTPService Objects in Adobe Flex/AIR

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

问题描述

我想请求需要通过Adobe AIR应用程序中的基本授权头一个HTTP资源。我尝试手动添加头的要求,以及使用setRemoteCredentials()方法来设置他们,但没有成功。

这里的code:

 < MX:脚本>
    <![CDATA [
    进口mx.rpc.events.ResultEvent;
    进口mx.rpc.events.FaultEvent;

    私有函数authAndSend(服务:的HTTPService):无效
    {
    service.setRemoteCredentials('someusername','somepassword');
    service.send();
    }

    私有函数resultHandler(事件:的ResultEvent):无效
    {
    apiResult.text = event.result.toString();
    }

    私有函数resultFailed(事件:的FaultEvent):无效
    {
    apiResult.text = event.fault.toString();
    }
    ]]≥
< / MX:脚本>

< MX:的HTTPService的id =apiService
    URL =htt​​ps://mywebservice.com/someFileThatRequiresBasicAuth.xml
    的resultFormat =文本
    结果=resultHandler(事件)
    故障=resultFailed(事件)/>

< MX:按钮的ID =apiButton
    标签=测试API命令
    点击=authAndSend(apiService)/>

< MX:文本ID =apiResult/>
 

然而,一个标准的基本身份验证对话框仍然会弹出提示他们的用户名和密码。我有一种感觉,我不这样做的正确的方式,但所有的信息,我能找到(的Flex文档,博客,谷歌等),要么没有工作或太含糊帮助。

任何黑魔法,哦Flex的大师?谢谢你。


编辑:更改setRemoteCredentials()来setCredentials方法()产生以下的ActionScript错误:

  [MessagingError消息=身份验证不支持DirectHTTPChannel(无代理)。]
 


编辑:问题解决了,之后从Adobe的一些关注。请参阅下面的帖子更全面的解释。这code会为任意长度的HTTP认证头。

 进口mx.utils.Base64En codeR;
私有函数authAndSend(服务:的HTTPService):无效
{
        变种EN codeR:Base64En codeR =新Base64En codeR();
        EN coder.insertNewLines = FALSE; //见下面为什么你需要这样做
        EN coder.en code(someusername:somepassword);

        service.headers = {授权:基本+ EN coder.toString()};
        service.send();
}
 

解决方案

终于得到了一些关注,Adobe和得到这个答案。长HTTP认证头的问题是,默认情况下,Base64En codeR类将注入换行符每72个字符。明显导致基部64烯$ C $光盘串的块是PTED作为一个新的标题的属性,这将导致误差间$ P $

您可以通过(在上面的例子)EN coder.insertNewLines = FALSE设置解决这个问题;默认设置为true。

我已经解决了上述code到工作任意长身份验证字符串。

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.

Here's the code:

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

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.

Any black magic, oh Flex gurus? Thanks.


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

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


EDIT: 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();
}

解决方案

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.

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.

这篇关于HTTP基本认证使用的HTTPService在Adobe Flex对象/ AIR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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