如何防止Flash的URLRequest转义url?

时间:2020-03-05 18:53:20  来源:igfitidea点击:

我从Flex应用程序的servlet加载一些XML,如下所示:

_loader = new URLLoader();
_loader.load(new URLRequest(_servletURL+"?do=load&id="+_id));

我们可以想象_servletURL就像http://foo.bar/path/to/servlet

在某些情况下,此URL包含带重音符号(长篇小说)。我将unscaped字符串传递给URLRequest,但是flash似乎对其进行了转义并调用了转义的URL,这是无效的。有想法吗?

解决方案

回答

我不确定这是否会有所不同,但这是实现相同URLRequest的一种更简洁的方法:

var request:URLRequest = new URLRequest(_servletURL)
request.method = URLRequestMethod.GET;
var reqData:Object = new Object();

reqData.do = "load";
reqData.id = _id;
request.data = reqData;

_loader = new URLLoader(request);

回答

从livedocs:http://livedocs.adobe.com/flex/3/langref/flash/net/URLRequest.html

Creates a URLRequest object. If System.useCodePage is true, the request is encoded using the system code page, rather than Unicode. If System.useCodePage is false, the request is encoded using Unicode, rather than the system code page.

此页面包含更多信息:http://livedocs.adobe.com/flex/3/html/help.html?content=18_Client_System_Environment_3.html

但基本上我们只需要将此添加到将在URLRequest之前运行的函数中(我可能会将其放在creationComplete事件中)

System.useCodePage = false;

回答

我的朋友路易斯发现了:

我们应该使用encodeURI进行UTF8URL编码
http://livedocs.adobe.com/flex/3/langref/package.html#encodeURI()

但不转义,因为它转义为ASCII,请参见
http://livedocs.adobe.com/flex/3/langref/package.html#unescape()

我认为这是我们在URL中获得%E9而不是预期的%C3%A9的地方。

http://www.w3schools.com/TAGS/ref_urlencode.asp