flash.net
Classpublic final class URLRequest
继承URLRequest Inheritance Object

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0, Flash Player 9

URLRequest 类可捕获单个 HTTP 请求中的所有信息。将 URLRequest 对象传递给 Loader、URLStream 和 URLLoader 类以及其他加载操作的 load() 方法以启动 URL 下载,并传递给 FileReference 类的 upload()download() 方法。

在运行于 Flash Player 的内容中使用此类时,请考虑以下安全模型:

但在 Adobe AIR 中,应用程序安全沙箱中的内容(使用 AIR 应用程序安装的内容)不受这些安全限制的约束。

对于在 Adobe AIR 中运行的内容,应用程序安全沙箱中的文件(使用 AIR 应用程序安装的文件)可以通过使用以下任一 URL 架构访问 URL:

在 Adobe AIR 中运行且不在应用程序安全域中的内容与在浏览器(在 Flash Player 中)中运行的内容遵循相同的限制,加载操作受内容的域 和跨域策略文件中授予的任何权限控制

有关详细信息,请参阅以下部分:

查看示例

另请参见

flash.html.HTMLLoader
flash.net.FileReference
flash.net.URLLoader
flash.net.URLRequestHeader
flash.net.URLRequestDefaults
flash.net.URLStream
Working with external data


公共属性
 属性定义方
  AIR-only authenticate : Boolean
指定是否应为此请求处理验证请求,如果是,则为 true,否则为 false。
URLRequest
  AIR-only cacheResponse : Boolean
指定是否应为此请求缓存成功的响应数据。
URLRequest
 Inheritedconstructor : Object
对类对象或给定对象实例的构造函数的引用。
Object
  contentType : String
data 属性中内容的 MIME 内容类型。
URLRequest
  data : Object
一个对象,它包含将随 URL 请求一起传输的数据。
URLRequest
  digest : String
唯一地标识将存储到 Flash Player 缓存中(或从中检索)的已签名 Adobe 平台组件的字符串。
URLRequest
  AIR-only followRedirects : Boolean
指定是否要遵循重定向,如果是,则为 true,否则为 false。
URLRequest
  AIR-only manageCookies : Boolean
指定 HTTP 协议堆栈是否应管理此请求的 cookie。
URLRequest
  method : String
控制 HTTP 式提交方法。
URLRequest
 Inheritedprototype : Object
[] 对类或函数对象的原型对象的引用。
Object
  requestHeaders : Array
要追加到 HTTP 请求的 HTTP 请求标头的数组。
URLRequest
  url : String
所请求的 URL。
URLRequest
  AIR-only useCache : Boolean
指定在此 URLRequest 获取数据之前是否应查询本地缓存。
URLRequest
  AIR-only userAgent : String
指定要在 HTTP 请求中使用的用户代理字符串。
URLRequest
公共方法
 方法定义方
  
URLRequest(url:String = null)
创建 URLRequest 对象。
URLRequest
 Inherited
指示对象是否已经定义了指定的属性。
Object
 Inherited
指示 Object 类的实例是否在指定为参数的对象的原型链中。
Object
 Inherited
指示指定的属性是否存在、是否可枚举。
Object
 Inherited
设置循环操作动态属性的可用性。
Object
 Inherited
返回指定对象的字符串表示形式。
Object
 Inherited
返回指定对象的原始值。
Object
属性详细信息
AIR-only authenticate属性
authenticate:Boolean  []

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0

指定是否应为此请求处理身份验证请求,如果是,则为 true,否则为 false。如果为 false,则验证挑战返回 HTTP 错误。

支持的身份验证方法有:

注意FileReference.upload()FileReference.download()HTMLLoader.load() 方法不支持 URLRequest.authenticate 属性。

默认值为 true.


实现
    public function get authenticate():Boolean
    public function set authenticate(value:Boolean):void

引发
SecurityError — 调用方不在 AIR 应用程序安全沙箱中。

另请参见

AIR-only cacheResponse属性 
cacheResponse:Boolean  []

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0

指定是否应为此请求缓存成功的响应数据。设置为 true 时,AIR 应用程序将使用操作系统的 HTTP 缓存。

注意HTMLLoader.load() 方法不支持 URLRequest.cacheResponse 属性。

默认值为 true.


实现
    public function get cacheResponse():Boolean
    public function set cacheResponse(value:Boolean):void

引发
SecurityError — 调用方不在 AIR 应用程序安全沙箱中。

另请参见

contentType属性 
contentType:String  []

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0, Flash Player 9

data 属性中内容的 MIME 内容类型。

注意:FileReference.upload()FileReference.download()HTMLLoader.load() 方法不支持 URLRequest.contentType 属性。


实现
    public function get contentType():String
    public function set contentType(value:String):void

另请参见

data属性 
data:Object  []

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0, Flash Player 9

一个对象,它包含将随 URL 请求一起传输的数据。

该属性与 method 属性一起使用。在 Adobe AIR 中,当使用 GET 以外的任意 HTTP 方法时发送数据。在 Flash Player 中,当使用 POST HTTP 方法时发送数据。

URLRequest API 支持二进制 POST,并支持 URL 编码变量和字符串。该数据对象可以是 ByteArray、URLVariables 或 String 对象。

该数据的使用方式取决于所用对象的类型:

在方法(如 navigateToURL()FileReference.upload())使用 URLRequest 对象前,不会发送该数据。


实现
    public function get data():Object
    public function set data(value:Object):void

另请参见


示例
下例在一个新的浏览器窗口中打开位于 http://www.[yourDomain].com/application.jsp 上的远程应用程序,并将 URLVariables 对象中捕获的有关用户会话的数据传递给该应用程序。

该示例的要点遵循:

  1. 构造函数会创建名为 request 的 URLRequest 实例,同时将远程应用程序的 URL 作为参数。
  2. 创建了一个 URLVariables 对象,并对它的两个属性进行了赋值。
  3. URLVariables 对象被赋给了 URLRequest 对象的 data 属性。
  4. 该示例调用 navigateToURL,在一个新的浏览器窗口中打开远程应用程序的 URL。

注意:若要运行该示例,必须将该示例中的远程应用程序 URL 替换为工作 URL。此外,需要用服务器代码处理 Flash Player 在 URLVariables 对象中捕获的信息。

package {
    import flash.display.Sprite;
    import flash.net.navigateToURL;
    import flash.net.URLRequest;
    import flash.net.URLVariables;

    public class URLVariablesExample extends Sprite {

        public function URLVariablesExample() {
            var url:String = "http://www.[yourDomain].com/application.jsp";
            var request:URLRequest = new URLRequest(url);
            var variables:URLVariables = new URLVariables();
            variables.exampleSessionId = new Date().getTime();
            variables.exampleUserLabel = "guest";
            request.data = variables;
            navigateToURL(request);
        }
    }
}
digest属性 
digest:String  []

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0, Flash Player 9.0.115.0

唯一地标识将存储到 Flash Player 缓存中(或从中检索)的已签名 Adobe 平台组件的字符串。摘要对应于单个缓存文件;如果对该文件进行了任何更改,则其摘要的更改将以不可预知的方式进行。通过使用摘要,您可以跨多个域验证缓存文件。具有相同摘要的两个文件是同一个文件,具有不同摘要的两个文件是不同的文件。实际上无法创建文件来“窃听”摘要以假装是另一个摘要。此属性仅适用于 SWF 内容,不适用于在 AIR 中运行的 JavaScript 代码。

摘要基于 SHA-256 消息摘要算法(长度为 64 个字符,采用十六进制格式)。


实现
    public function get digest():String
    public function set digest(value:String):void

引发
ArgumentError — 提供的摘要与从下载的签名文件或从缓存中加载的签名文件中提取的文件摘要不匹配。如果提供的摘要长度错误或包含无效(非十六进制)字符,则 Flash Player 也会引发此错误。

示例
下面的示例将远程文件加载到缓存中。加载结束时,字节数组包含实际的文件(而不是签名的文件)。该示例通过调用 loadBytes() 完成加载操作:
 
var myURLReq:URLRequest = new URLRequest();
myURLReq.url = "http://yourdomain/users/jdoe/test01/_rsc/Automated/AssetCaching_rsc/test01/rsl.swz";
myURLReq.digest = "3B0AA28C7A990385E044D80F5637FB036317BB41E044D80F5637FB036317BB41";
var myURLLoader:URLLoader = new URLLoader();
myURLLoader.dataFormat = URLLoaderDataFormat.BINARY;
myURLLoader.addEventListener("complete", onC);

myURLLoad.load(myURLReq);

function onC(e) {
    var someLoader:Loader = new Loader();
    addChild(someLoader);
    someLoader.loadBytes((ByteArray)(myURLLoad.data)); 
}

AIR-only followRedirects属性 
followRedirects:Boolean  []

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0

指定是否要遵循重定向,如果是,则为 true,否则为 false

注意FileReference.upload()FileReference.download()HTMLLoader.load() 方法不支持 URLRequest.followRedirects 属性。

默认值为 true.


实现
    public function get followRedirects():Boolean
    public function set followRedirects(value:Boolean):void

引发
SecurityError — 调用方不在 AIR 应用程序安全沙箱中。

另请参见

AIR-only manageCookies属性 
manageCookies:Boolean  []

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0

指定 HTTP 协议堆栈是否应管理此请求的 cookie。如果为 true,则将 cookie 添加到请求中并记住响应 cookie。如果为 false,则 将 cookie 添加到请求中且 记住响应 cookie,但用户可以通过直接处理标头来自行管理 cookie。

在 Mac OS 上,cookie 与 Safari 共享。清除 Mac OS 上的 cookie:

  1. 打开 Safari。
  2. 选择“Safari”>“首选参数”,然后单击“安全”面板。
  3. 单击“显示 Cookie”按钮。
  4. 单击“全部删除”按钮。

清除 Windows 上的 cookie:

  1. 打开“Internet 属性”控制面板并单击“常规”选项卡。
  2. 单击“删除 Cookie”按钮。

默认值为 true.


实现
    public function get manageCookies():Boolean
    public function set manageCookies(value:Boolean):void

引发
SecurityError — 调用方不在 AIR 应用程序安全沙箱中。

另请参见

method属性 
method:String  []

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0, Flash Player 9

控制 HTTP 式提交方法。

对于在 Flash Player 中(在浏览器中)运行的 SWF 内容,此属性限制为 GETPOST 操作,有效值为 URLRequestMethod.GETURLRequestMethod.POST对于在 Adobe AIR 中运行的内容,如果内容位于 application 安全域中,则可以使用任意字符串值;否则(如果内容在 AIR application 安全域中),仍将限于使用 GETPOST

注意:如果在 Flash Player 中运行并且引用的表单没有正文,则即使未将该方法设置为 URLRequestMethod.POST,Flash Player 也会自动使用 GET 操作。

默认值为 URLRequestMethod.GET.


实现
    public function get method():String
    public function set method(value:String):void

引发
ArgumentError — 如果 value 参数不是 URLRequestMethod.GETURLRequestMethod.POST

另请参见


示例
下例在一个新的浏览器窗口中打开位于 http://www.[yourDomain].com/application.jsp 上的远程应用程序,并将 URLVariables 对象中捕获的有关用户会话的数据传递给该应用程序。它将 URLRequest.method 属性值显式设置为 URLRequestMethod.POST

该示例的要点遵循:

  1. 构造函数会创建名为 request 的 URLRequest 实例,同时将远程应用程序的 URL 作为参数。
  2. 创建了一个 URLVariables 对象,并对它的两个属性进行了赋值。
  3. URLVariables 对象被赋给了 URLRequest 对象的 data 属性。
  4. URLRequest.method 属性的值设置为 URLRequestMethod.POST
  5. 该示例调用 navigateToURL,在一个新的浏览器窗口中打开远程应用程序的 URL。

注意:若要运行该示例,必须将该示例中的远程应用程序 URL 替换为工作 URL。此外,需要用服务器代码处理 Flash Player 在 URLVariables 对象中捕获的信息。

package {
    import flash.display.Sprite;
    import flash.net.navigateToURL;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.URLVariables;

    public class URLRequest_method extends Sprite {

        public function URLRequest_method() {
            var url:String = "http://www.[yourDomain].com/application.jsp";
            var request:URLRequest = new URLRequest(url);
            
            var variables:URLVariables = new URLVariables();
            variables.exampleSessionId = new Date().getTime();
            variables.exampleUserLabel = "guest";
            request.data = variables;
            request.method = URLRequestMethod.POST;
            
            navigateToURL(request);
        }
    }
}
requestHeaders属性 
requestHeaders:Array  []

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0, Flash Player 9

要追加到 HTTP 请求的 HTTP 请求标头的数组。该数组由 URLRequestHeader 对象组成。数组中的每一对象必须是包含一个名称字符串和一个值字符串的 URLRequestHeader 对象,如下所示:

  var rhArray:Array = new Array(new URLRequestHeader("Content-Type", "text/html"));
  

Flash Player 和 AIR 运行时对请求标头强加某些限制;有关详细信息,请参阅 URLRequestHeader 类的描述。

FileReference.upload()FileReference.download() 方法不支持 URLRequest.requestHeaders 参数。


实现
    public function get requestHeaders():Array
    public function set requestHeaders(value:Array):void

另请参见

url属性 
url:String  []

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0, Flash Player 9

所请求的 URL。

对于在 Flash Player 中运行的内容和在 Adobe AIR 中运行且不在 application 安全沙箱中的内容,默认情况下,URL 必须与调用文件位于完全相同的域中,包括子域。例如,分别位于 www.adobe.com 和 store.adobe.com 中的文件的域不同。

确保对统一资源定位器规范(请参阅 http://www.faqs.org/rfcs/rfc1738.html)中说明为不安全的字符或 URLRequest 对象的 URL 架构中保留的字符(如果未用于其保留用途)进行编码。例如,按照“http://www.example.com/orderForm.cfm?item=%23B-3&discount=50%25”的规定,对百分比符号 (%) 使用“%25”,对数字符号 (#) 使用“%23”

若要从其它域中加载数据,请在承载数据的服务器上放置一个跨域策略文件。有关详细信息,请参阅 URLRequest 类说明中所述的安全性文档。此限制不适用于在应用程序安全沙箱中运行的 AIR 内容。

对于在 Adobe AIR 中运行的内容,应用程序安全域中的文件(随 AIR 应用程序一起安装的文件)可以通过使用以下任一 URL 架构访问 URL:

注意:AIR 和 Flash Player 9.0.115.0 及其更高版本均支持 IPv6(Internet 协议版本 6)。IPv6 是支持 128 位地址的 Internet 协议版本(它是支持 32 位地址的早期 IPv4 协议的改进版本)。您可能需要在网络接口中激活 IPv6。有关详细信息,请参阅承载数据的操作系统的帮助。如果承载系统中支持 IPv6,您可以在用方括号 ([]) 括起的 URL 中指定数字 IPv6 文本地址,如下所示:

rtmp://[2001:db8:ccc3:ffff:0:444d:555e:666f]:1935/test


实现
    public function get url():String
    public function set url(value:String):void
AIR-only useCache属性 
useCache:Boolean  []

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0

指定在此 URLRequest 获取数据之前是否应查询本地缓存。

注意HTMLLoader.load() 方法不支持 URLRequest.useCache 属性。

默认值为 true.


实现
    public function get useCache():Boolean
    public function set useCache(value:Boolean):void

引发
SecurityError — 调用方不在 AIR 应用程序安全沙箱中。

另请参见

AIR-only userAgent属性 
userAgent:String  []

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0

指定要在 HTTP 请求中使用的用户代理字符串。

默认值是由 Flash Player 使用的同一用户代理字符串,该字符串在 Mac 和 Windows 上不同。

注意:在将 URLRequest 对象与 HTMLLoader 对象的 load() 方法一起使用时,此属性不影响用户代理字符串。若要设置 HTMLLoader 对象的用户代理字符串,请设置 HTMLLoader 对象的 userAgent 属性或设置静态 URLRequestDefaults.userAgent 属性。


实现
    public function get userAgent():String
    public function set userAgent(value:String):void

引发
SecurityError — 调用方不在 AIR 应用程序安全沙箱中。

另请参见

构造函数详细信息
URLRequest()构造函数
public function URLRequest(url:String = null)

语言版本 : ActionScript 3.0
RuntimeVersions: AIR 1.0, Flash Player 9

创建 URLRequest 对象。如果 System.useCodePagetrue,则使用系统代码页对该请求进行编码,而不使用 Unicode。如果 System.useCodePagefalse,则使用 Unicode 对该请求进行编码,而不使用系统代码页。

参数
url:String (default = null) — 所请求的 URL。随后可以使用 url 属性来设置 URL。

另请参见

示例 如何使用示例
URLRequestExample.as

下例创建一个新的 Loader 对象,并为其传递一个包含 XML 文件路径的 URLRequest 对象。如果加载操作成功,将调度 complete 事件,并且 XML 文件中的数据会跟踪至输出。附加的事件处理函数会捕获其它事件,包括错误事件。

若要运行该示例,请将名为 XMLFile.xml 的文件与 SWF 文件放在同一个目录下。


package {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.*;

    public class URLRequestExample extends Sprite {

        public function URLRequestExample() {
            var loader:URLLoader = new URLLoader();
            configureListeners(loader);

            var request:URLRequest = new URLRequest("XMLFile.xml");
            try {
                loader.load(request);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
        }

        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }

        private function completeHandler(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
            trace("completeHandler: " + loader.data);
        }

        private function openHandler(event:Event):void {
            trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }
    }
}




 

评论添加到页面后给我发送电子邮件 | 评论报告

当前页: http://livedocs.adobe.com/flex/3_cn/flash/net/URLRequest.html