Problem Summary: Once I need to get the host, protocol, port, parameters from a given URL with Action-Script. I googled for a class named HTTPUtil developed by members of flexpasta.com, and got some tips about URLUtil of Flex SDK. However, HTTPUtil is used to resolve information from the URL of the page the SWF file embed in. URLUtil is useful, but a little complex. I need a simple method to resolve a given URL, and use the resolved information directly without extra processing.
Solution Summary: I decide to write a class named URLParser myself to satisfy my requirement using regular expression of action-script.
- package
- {
- import mx.managers.ISystemManager;
- [Bindable]
- public class URLParser
- {
- public static var url:String;
- public static var host:String = "";
- public static var port:String = "";
- public static var protocol:String = "";
- public static var path:String = "";
- public static var parameters:Object;
- public static function parse( url:String ) : void
- {
- URLParser.url = url;
- var reg:RegExp = /(?P<protocol>[a-zA-Z]+) : \/\/ (?P<host>[^:\/]*) (:(?P<port>\d+))? ((?P<path>[^?]*))? ((?P<parameters>.*))? /x;
- var results:Array = reg.exec(url);
- protocol = results.protocol
- host = results.host;
- port = results.port;
- path = results.path;
- var paramsStr:String = results.parameters;
- if(paramsStr!="")
- {
- parameters = null;
- parameters = new Object();
- if(paramsStr.charAt(0) == "?")
- {
- paramsStr = paramsStr.substring(1);
- }
- var params:Array = paramsStr.split("&");
- for each(var paramStr:String in params)
- {
- var param:Array = paramStr.split("=");
- parameters[param[0]] = param[1];
- }
- }
- }
- }
- }
Description: I use regular expression to resolve what I want from the given URL. The explanation of the expression is below:
As the url is “http://localhost:8080/URLParserDemo.swf?debug=true”
- (?P<protocol>[a-zA-Z]+) match the protocol
- http
- : \/\/ match “://”
- ://
- (?P<host>[^:\/]*) match the host domain
- localhost
- (:(?P<port>\d+))? match the port, if any
- :8080
- ((?P<path>[^?]*))? match the requested resource path of the host, if any
- URLParserDemo.swf
- ((?P<parameters>.*))? match the request parameters, if any
- ?debug=true
Following is the source of URLParserDemo.mxml:
- <?xml version="1.0" encoding="utf-8"?>
- <!-- http://ntt.cc -->
- <mx:Application
- xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="absolute"
- creationComplete="initApp()"
- >
- <mx:Script>
- <![CDATA[
- import mx.controls.Alert;
- import mx.utils.ObjectUtil;
- [Bindable]
- private var params:Array;
- private function initApp():void
- {
- URLParser.parse(systemManager.loaderInfo.url);
- var classInfo:Object = ObjectUtil.getClassInfo(URLParser.parameters);
- if(classInfo.properties!=null && classInfo.properties.length>0)
- params = classInfo.properties;
- }
- ]]>
- </mx:Script>
- <mx:Panel width="100%" height="100%" title="HttpUtil Demo窶披粘how the host info of the loaded swf">
- <mx:Form width="100%" height="100%">
- <mx:FormItem label="url">
- <mx:Label text="{ URLParser.url}"/>
- </mx:FormItem>
- <mx:FormItem label="host">
- <mx:Label text="{ URLParser.host}"/>
- </mx:FormItem>
- <mx:FormItem label="path">
- <mx:Label text="{ URLParser.path}"/>
- </mx:FormItem>
- <mx:FormItem label="port">
- <mx:Label text="{ URLParser.port}"/>
- </mx:FormItem>
- <mx:FormItem label="protocol">
- <mx:Label text="{ URLParser.protocol}"/>
- </mx:FormItem>
- <mx:FormItem label="parameters" direction="horizontal">
- <mx:Repeater dataProvider="{params}" id="paramList">
- <mx:Label text="{paramList.currentItem.localName}"/>
- <mx:Label text="{URLParser.parameters[paramList.currentItem.localName]}"/>
- </mx:Repeater>
- </mx:FormItem>
- </mx:Form>
- </mx:Panel>
- </mx:Application>
*You can find more HTTPUtil info on: http://www.flexpasta.com

August 7th, 2008
Ntt.cc 








Posted in
Tags: 
RSS Feed
Email Feed
Great class! I like it more than mx.utils.URLUtil which does the same type of thing but on a smaller scale.
I am using URLUtil for the parameters though. If your URL is “something.html?id=3&debug=true” you can use
var params:String = URLUtil.objectToString(URLParser.parameters, “&”, true);
This will result in “id=3&debug=true”
[...] http://ntt.cc/2008/08/07/urlparser-a-parser-to-get-value-of-host-protocol-port-parameters-from-an-ur... カテゴリー: as3, flex入門 タグ: as3, flex3, url解析 コメント (0) トラックバック (0) コメントをどうぞ トラックバックURL [...]