Click here to Skip to main content
15,886,873 members
Articles / Web Development / HTML

Retrieve information about your IP

Rate me:
Please Sign up or sign in to vote.
3.89/5 (9 votes)
10 Mar 2013CPOL3 min read 36.7K   2.2K   18  
Retrieve information about your IP from a website.
/// ****************************************
/// * 		    Created by bEGI	           *
/// *  http://www.youtube.com/user/MCneun  *
/// *	       VB & C# Programmer	       *
/// ****************************************



using System;
using System.IO;
using System.Net;
using System.Data;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;


namespace IP_Informations
{
    public partial class IPInfo : Form
    {
        string lookupData;

        public IPInfo()
        {
            InitializeComponent();
        }

        private void btLookup_Click(object sender, EventArgs e)
        {
            getIPInfos();
        }

        private void getIPInfos()
        {
            btLookup.Enabled = false;
            try
            {
                Uri ur = new Uri("http://www.find-ip-address.org/");
                System.Net.HttpWebRequest req = (System.Net.HttpWebRequest) System.Net.WebRequest.Create(ur);
                System.Net.HttpWebResponse res = (System.Net.HttpWebResponse) req.GetResponse();
                if (res.StatusCode == HttpStatusCode.OK)
                {
                    Stream receiveStream = res.GetResponseStream();
                    StreamReader readStream = null;
                    if (res.CharacterSet == null)
                    {
                        readStream = new StreamReader(receiveStream);
                    }
                    else
                    {
                        readStream = new StreamReader(receiveStream, Encoding.GetEncoding(res.CharacterSet));
                        string data = readStream.ReadToEnd();
                        res.Close();
                        readStream.Close();
                        string left = "My IP Country Name:";
                        string right = "IP Address Lookup Location";
                        int indexLeft = data.IndexOf(left);
                        int indexRight = data.IndexOf(right);
                        lookupData = data.Substring(indexLeft + left.Length, indexRight - indexLeft - left.Length);
                    }
                }

                labelCountry.Text = extractSubject(":&nbsp;&nbsp;<font color='#980000'> ", "</font>&nbsp;&nbsp;<img src='"); //COUNTRY NAME
                string myStr = extractSubject("</font>&nbsp;&nbsp;<img src='", "'><br><strong>My IP Country Continent<");
                string[] countryCode = myStr.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                labelCountry.Text += "(" + countryCode[2].Remove(2).ToUpper() + ")";
                textBoxExternal.Text = extractSubject(">My IP Address lookup</strong> for <b>", "</b> show IP which"); // MY IP ADDRESS
                textBoxInternal.Text = getInternalIP();
                textBoxCity.Text = extractSubject("City</b>:&nbsp;&nbsp; <font color='#980000'>", "</font><br><b>My IP Address Latitude</b>"); // IP ADDRESS CITY
                string tbRegion = extractSubject(")<br><br><strong>My IP Address Region</strong>: <font color='#980000'>", "</font><br><b>"); // MY ADDRESS REGION
                string[] str = tbRegion.Split('<');
                textBoxRegion.Text = str[0];
                textBoxISP.Text = extractSubject("Provider)</strong>:&nbsp;<font color='#980000'> ", "</font><br /"); // MY ISP PROVIDER
                textBoxTimeZone.Text = extractSubject("My Time zone</b>: ", "<br><b>My Local time"); // MY TIME ZONE
                textBoxLat.Text = extractSubject("My IP Address Latitude</b>: ", "<br><b>My IP Address Longtitude"); // MY IP ADDRESS LATITUDE
                textBoxLng.Text = extractSubject("My IP Address Longtitude</b>: ", "<br><br><strong>My ISP"); // MY IP ADDRESS LONGTITUDE

                pictureBoxFlag.Load("http://www.find-ip-address.org/" + extractSubject("</font>&nbsp;&nbsp;<img src='", "'><br><strong>My IP Country Continent<")); // COUNTRY FLAG
            }
            catch
            {
            }
            btLookup.Enabled = true;
        }

        private string extractSubject(string left, string right)
        {
            try
            {
                int indexLeft = lookupData.IndexOf(left);
                int indexRight = lookupData.IndexOf(right);
                return lookupData.Substring(indexLeft + left.Length, indexRight - indexLeft - left.Length);
            }
            catch
            {
                return "Unable to retrieve!";
            }
        }

        private string getInternalIP()
        {
            try
            {
                IPHostEntry iphostentry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName());
                return ((System.Net.IPAddress)iphostentry.AddressList.GetValue(0)).ToString();
            }
            catch
            {
                return "Unable to retrieve!";
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions