RSS

Validating if the url exists or not on Client side with FLEX

29 Jul

Yup, Quite interesting isn’t it ?

Mission: Validate URL Existance on client side.

Expected Result: Anyhow create POC of a code with the efforts to check the possibility of verifying URL existence from Flex i.e. from client side only.

Result: . I think i managed to crack it with a simple solution, When code not deployed works very smooth and gives result as expected.

On Local Environment Without server deployment of code

Result is great !

But at last Buck Stops at “Security Sandbox Error” when we deployed it on to the server.

Path Followed:

Knowing flash.net package : The flash.net package contains classes for sending and receiving from the network, such as URL downloading and Flash Remoting.

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/package-detail.html

Understand HTTP response status meanings

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

With Some 2-3 days reading i just went on to build one example with URL existence verification application.

If Somebody is intrested to solve this puzzle, take the below code.

Project Hierarchy:

src-assets-HttpCodes.xml

src-ValidateURL.mxml

Lets Walk Through the code, I am sure the written code is pretty simple to understand so wont be elaborating it.

Code for “ValidateURL.mxml


<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:net="flash.net.*"
layout="absolute"
creationComplete="httpIOError.send();">

<mx:Script>
<![CDATA[

import mx.controls.Alert;
import mx.utils.ObjectUtil;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import flash.net.*;

private var httpCode:int;

private var errorCodesAC:ArrayCollection = new ArrayCollection();

private function loadURL():void
{
var urlReq:URLRequest = new URLRequest('http://'+ urlString.text);
urlStream.load(urlReq);
}

private function checkUrl(e:HTTPStatusEvent):void
{
httpCode=e.status;
}

private function ioErrorHandler(e:IOErrorEvent):void
{
for(var i:int=0;i<errorCodesAC.length;i++)
{
if(httpCode==errorCodesAC[i].number)
{
responseText.text = "Status Code: "+httpCode.toString() + "\nError Details: \n"+errorCodesAC[i].details;
}
}
}

private function returnSuccess(e:Event):void
{
responseText.text = "Status Code: "+httpCode.toString() + "\nURL is valid."
}

private function securityErrorHandler(e:SecurityErrorEvent):void
{
Alert.show(ObjectUtil.toString(e),"Security Error Details");
}

private function faultHandler(e:FaultEvent):void
{
Alert.show(ObjectUtil.toString(e));
}

private function resultHandler(e:ResultEvent):void
{
errorCodesAC = e.result.HttpStatus.statusCodes.statusCode
}

]]>
</mx:Script>

<mx:HTTPService id="httpIOError" url="assets/HttpCodes.xml" result="resultHandler(event)" fault="faultHandler(event)"/>

<net:URLStream  id="urlStream"  complete="returnSuccess(event)" httpStatus="checkUrl(event)"
ioError="ioErrorHandler(event)" securityError="securityErrorHandler(event)" />

<mx:Label x="135" y="30" text="Enter Url to be validated:"/>
<mx:Button x="321" y="84" label="Check URL" click="loadURL()"/>
<mx:TextInput id="urlString"  x="288" y="28"/>
<mx:Text x="288" y="58" text="eg: www.shashankkulkarni.wordpress.com"/>
<mx:TextArea  id="responseText" x="135" y="133" height="93" width="414" editable="false" wordWrap="true"/>
<mx:Label x="135" y="107" text="Response Description:"/>
</mx:Application>

 “HttpCodes.xml” is as below


<HttpStatus>

<statusCodes>

<statusCode number="400">
<details>
Unable to understand request.[Bad Request]
</details>
</statusCode>

<statusCode number="401">
<details>
User authentication required. [Unauthorized Request]
</details>
</statusCode>

<statusCode number="403">
<details>
Server refuses the response. [Forbidden Request]
</details>
</statusCode>

<statusCode number="404">
<details>
Page Not Found. [Page Not Found ]
</details>
</statusCode>

<statusCode number="407">
<details>
Proxy Authentication required. [Proxy Authentication]
</details>
</statusCode>

<statusCode number="408">
<details>
Request failed to reach server within specified time. [Request Timeout]
</details>
</statusCode>

<statusCode number="500">
<details>
Unexpected condition occured. [Internal Server Error]
</details>
</statusCode>

<statusCode number="501">
<details>
Functionality required is not supported. [Functionality not supported]
</details>
</statusCode>

<statusCode number="502">
<details>
Invalid response received by server. [Bad Gateway]
</details>
</statusCode>

<statusCode number="503">
<details>
Service is not available temporarily.[Service Unavailable]
</details>
</statusCode>

<statusCode number="504">
<details>
Timely response not received. [Gateway Timeout]
</details>
</statusCode>

<statusCode number="0">
<details>
Input stream is not well Formatted [IOError]
</details>
</statusCode>

<statusCode number="200">
<details>
Yes! This URL exists. [Success]
</details>
</statusCode>

</statusCodes>

</HttpStatus>

I gone through details of Sandbox Security restriction with Flash player ( http://www.adobe.com/devnet/flashplayer/articles/flash_player_9_security.pdf ) but unfortunatly could not solve it to success,  i really feel this should have been done from Flex itself.

I would have been happy to see it working, But I guess some more understanding is needed on my part. I am still finding the missing things.

There must be some way to DO it ???? Still searching the answer, If some body finds it… Just ping it down here. I will be glad to resolve it.

Any suggestions are welcomed.

Best Luck & Happy coding :)

Thanks and regards

Shashank Kulkarni

Feel free to comment on the post :)

Advertisement
 

About shashankkulkarni

@Flex ,Fully loaded. Internet surfer, blogger.Post graduate from India
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.