简体   繁体   English

AS3 使用 Loader 加载受 htaccess 保护的文件

[英]AS3 Using a Loader to load a file protected by htaccess

I'm attempting to load an external SWF that's hosted on a site into a local SWF file.我正在尝试将站点上托管的外部 SWF 加载到本地 SWF 文件中。 The external SWF is in a password-protected directory using htaccess.外部 SWF 位于使用 htaccess 的受密码保护的目录中。

Here is the code I'm currently trying to use:这是我目前正在尝试使用的代码:

var loaderUrlRequest:URLRequest = new URLRequest("http://www.my-website.com/externalFlashFile.swf");
loaderUrlRequest.requestHeaders.push(new URLRequestHeader("Authorization", "Basic " + $Base64.encode("username:password")));
loaderUrlRequest.method = URLRequestMethod.POST;
loaderUrlRequest.data = true;
var loader:Loader = new Loader();
addChildAt(loader, 0);
loader.load(loaderUrlRequest);

The file URL, username, password, and base64 encoding are all checked and correct.文件URL、用户名、密码、base64编码都检查无误。

However, I'm getting the following errors:但是,我收到以下错误:

Error opening URL 'http://www.my-website.com/externalFlashFile.swf'
Error #2044: Unhandled IOErrorEvent:. text=Error #2036: Load Never Completed.

Any ideas what I might be doing wrong, or if there's a better way to go about this from a purely-flash perspective?任何想法我可能做错了什么,或者如果从纯闪存的角度来看,go 是否有更好的方法?

Thanks.谢谢。

White whale, holy grail 白鲸,圣杯

In Adobe® AIR®, content in the application security sandbox (such as content installed with the AIR application) can use any request headers, without error.在 Adobe® AIR® 中,应用程序安全沙箱中的内容(例如随 AIR 应用程序安装的内容)可以使用任何请求标头而不会出错。 However , for content running in Adobe AIR that is in a different security sandbox, or for content running in Flash® Player , using following request headers cause a runtime error :但是,对于在不同安全沙箱中的 Adobe AIR 中运行的内容,或在 Flash® Player 中运行的内容,使用以下请求标头会导致运行时错误

Accept-Charset, Accept-Encoding, Accept-Ranges, Age, Allow, Allowed, Authorization , Charge-To, Connect, Connection, Content-Length, Content-Location, Content-Range, Cookie, Date, Delete, ETag, Expect, Get, Head, Host, If-Modified-Since, Keep-Alive, Last-Modified, Location, Max-Forwards, Options, Origin, Post, Proxy-Authenticate, Proxy-Authorization, Proxy-Connection, Public, Put, Range, Referer, Request-Range, Retry-After, Server, TE, Trace, Trailer, Transfer-Encoding, Upgrade, URI, User-Agent, Vary, Via, Warning, WWW-Authenticate, x-flash-version.接受字符集,接受编码,接受范围,年龄,允许,允许,授权,收费,连接,连接,内容长度,内容位置,内容范围,Cookie,日期,删除,ETag,期望,获取,头,主机,If-Modified-Since,Keep-Alive,Last-Modified,位置,Max-Forwards,选项,来源,发布,代理认证,代理授权,代理连接,公共,放置,范围, Referer、Request-Range、Retry-After、Server、TE、Trace、Trailer、Transfer-Encoding、Upgrade、URI、User-Agent、Vary、Via、Warning、WWW-Authenticate、x-flash-version。

URLRequestDefaults.setLoginCredentialsForHost : Sets default user and password credentials for a selected host. URLRequestDefaults.setLoginCredentialsForHost :为选定主机设置默认用户和密码凭据。 ... Only Adobe® AIR® content running in the application security sandbox can use the URLRequestDefaults class. ...只有在应用程序安全沙箱中运行的 Adobe® AIR®内容才能使用 URLRequestDefaults class。

So, you have but one option: load your swf from a web page of that very domain that requires authorization.因此,您只有一个选择:从需要授权的域的 web 页面加载您的 swf。 You'll be through http authorization steps before swf is loaded and browser will maintain the rest.在加载 swf 之前,您将通过 http 授权步骤,并且浏览器将维护 rest。

In fact, when you try to access something behind http authorization, a server issues a http response with code=401 (unless you send Authorization header beforehand).实际上,当您尝试访问 http 授权后面的内容时,服务器会发出 http 响应,代码为 401(除非您事先发送 Authorization header)。 Flash Player can do nothing about it and only AIR runtime is capable of sending Authorization header in any way. Flash 播放器对此无能为力,只有 AIR 运行时能够以任何方式发送授权 header。

You need to authenticate from within your ActionScript application, which can be achieved by use of some static variables and methods on the URLRequestDefaults class.您需要在 ActionScript 应用程序中进行身份验证,这可以通过使用URLRequestDefaults class 上的一些 static 变量和方法来实现。

All you need to do, is add these two lines of code before you make the call with URLLoader:您需要做的就是在使用 URLLoader 进行调用之前添加这两行代码:

URLRequestDefaults.setLoginCredentialsForHost(hostname, user, password);
URLRequestDefaults.authenticate = true;

You can drop the requestHeaders too (unless they have another purpose).您也可以删除 requestHeaders(除非它们有其他目的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM