Search code examples
actionscript-3actionscriptnetwork-programmingair

Getting external IP using Adobe AIR


We can get external IP from this service using Java, C# or VB.NET. But I want to do that using Adobe AIR. How to make request to that link and get its string?


Solution

  • It will be like this:

    private function makeRequest():void
    {
        var loader:URLLoader = new URLLoader();
    
        configureListeners(loader);
        var req:URLRequest=new URLRequest("http://www.whatismyip.com/automation/n09230945.asp");
    
        try
    
        {   
            var header:URLRequestHeader=new URLRequestHeader("content-type", "text/plain");
    
            var header2:URLRequestHeader = new URLRequestHeader("pragma", "no-cache");
    
            req.requestHeaders.push(header);
            req.requestHeaders.push(header2);
    
            req.method = URLRequestMethod.POST;
            loader.dataFormat = URLLoaderDataFormat.TEXT;
            loader.load(req);
        }
        catch (error:Error)
        {
            trace("Unable to load requested document.");
        }
    }
    
    private function configureListeners(dispatcher:IEventDispatcher):void
    {
        dispatcher.addEventListener(Event.COMPLETE, completeHandler);
    }
    
    private function completeHandler(event:Event):void
    {
        var loader:URLLoader = URLLoader(event.target);
        mx.controls.Alert.show("" + loader.data);
    }