Gokul's Blog

Get My IP Simulation

Leave a comment

http://gokul.i3software.in/samplecode/ipaddress.aspx

 

Code following Shortly.

 

Code to get the IP from Webpage to a Console App

try
  {
   // Use a web page that displays the IP of the request.  
   WebRequest myRequest = WebRequest.Create("http://Myurl");

   // Send request, get response, and parse out the IP address on the page.
   using(WebResponse res = myRequest.GetResponse())
   {
    using(Stream s = res.GetResponseStream())
    using(StreamReader sr = new StreamReader(s, Encoding.UTF8))
    {
     string html = sr.ReadToEnd();
     Regex regex = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
     string ipString = regex.Match(html).Value;
     Console.WriteLine("Public IP: "+ipString);
    }
   }
  }
  catch(Exception ex)
  {
   Console.WriteLine("Error getting IP Address:\n"+ex.Message);
  }

Advertisements

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 )

Google+ photo

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

Connecting to %s