Macromedia Flex Macromedia Flex
Get client IP address with HTTPService call
  Home

Mar 08, 2007 - Get client IP address with HTTPService call
Sample uses an aspx page to return the client IP to flex

The Flex app calls the server-side url using HTTPService.  The sample .aspx (VB.NET) page gets the client IP address, wraps it in xml and returns it to Flex.  Flex reads the xml and displays the value.

Flex app:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
    creationComplete="initApp()">

<mx:Script><![CDATA[
  import mx.rpc.events.ResultEvent;
 
  [Bindable]private var _sClientIPUrl:String = "http://localhost/Sample/GetClientIP.aspx";
 
  /** Runs on creationComplete */
  private function initApp():void
 {
    dsClientIP.send();                    //invokes the data service call
 }
 
 /** called by the result event of the data service call */
 private function onResult(oEvent:ResultEvent):void
 {
   var xmlResult:XML = XML(oEvent.result);    //get the result as XML
   var sIp:String = xmlResult.@clientIP;      //get the attribute value
   lbIP.text = sIp;                           //display it
 }
]]></mx:Script> 
  <mx:Label id="lbIP" fontSize="26" />
  <mx:HTTPService id="dsClientIP"
     url="{_sClientIPUrl}"
     result="onResult(event)"
     resultFormat="e4x"/>
</mx:Application>

DOT.NET app: GetClientIP.aspx

<%@ Page Language="vb" Strict="True" ValidateRequest="False" %><%@ Page Language="vb" Strict="True" ValidateRequest="False" %>
<%Response.ContentType = "text/xml" %>

<SCRIPT language="vb" runat="server">
  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim sClientIP As String = Request.ServerVariables("remote_addr")  'Get the client ip address
    Dim sXML as String
    sXML = "<systemdata clientIP=""" & sClientIP & """ />"            'build the xml
    Response.Write(sXML)                                              'return it to the client
  End Sub  'Page_Load 
</SCRIPT> <%@ Page Language="vb" Strict="True" ValidateRequest="False" %><%Response.ContentType = "text/xml" %>

File Details
Created On Mar, 08, 2007 by Tracy Spratt
Last Modified On Mar, 08, 2007 by Tracy Spratt
Group: Tips and Articles
Flex Versions: 2.0
Category: General
Type: Complete Lesson
Difficulty: Intermediate
Keywords: