Getting IP Address of Host in C#/ASP.net
To read IP address of host using ASP.net, IPHostEntry class is used. We fetch IP of host into instance of IPHostEntry. This can be done by using GetHostEntry and. GetHostName methods of Dns class. GetHostName will return the host name in string format andGetHostEntry will resolve IP address to an IPHostEntry instance. Once these are resolved, IP addresses are stored in an array of type IPAddress using AddressList property. The entry at index 2 is the required host Ip Address.// Getting IP Address
IPHostEntry ip = Dns.GetHostEntry(Dns.GetHostName());
IPAddress[] hst = ip.AddressList;
IP.Text = hst[2].ToString(); //IP in IP.Text is asp label id
Reading a webpage using C#/ASP.net
To read a webpage of any website DownloadData method of WebClient class is used. Download data will download all the website page identified by url in parameters of DownloadData method in Byte array. Now simply transform that byte array into string.
The smart method is retrieving location information based on IP to your webpage from other websites offering the services. There are many such websites that will tell the location of the mentioned host IP. Google for it and you will find many. I personally will be using services of http://www.geobytes.com. Now we know how to download data from any webpage, lets start the digging in following steps.
System.Net.WebClient wc = new System.Net.WebClient();
byte[] data = wc.DownloadData("http://www.geobytes.com/IpLocator.htm?GetLocation&IpAddress="+ip);
string res = System.Text.Encoding.UTF8.GetString(data);
Finding location of host from IP in C#/ASP.net
Now to find location of host using host IP there are couple of methods. One is by hard work. Other one is by using smart work. The hard work method involves creating a database of IPs and storing individual location information with respect to each IP which is very tedious and frustrating so I am not going to do that.The smart method is retrieving location information based on IP to your webpage from other websites offering the services. There are many such websites that will tell the location of the mentioned host IP. Google for it and you will find many. I personally will be using services of http://www.geobytes.com. Now we know how to download data from any webpage, lets start the digging in following steps.
- Get Host Ip as mentioned above.
- Append that IP to string url http://www.geobytes.com/IpLocator.htm?GetLocation&IpAddress=
- Use DownloadData to download data from the aforementioned url and convert that to string. This will download the whole page not just the data required.
- Now its the time for some smart work to be done. Open the url in your browser. You know where is your location is mentioned in the opened page. Now view the source of web page. Search for your location say Jalandhar in the HTML code. It will be enclosed in double quotes. Copy some portion of code (at least 35-40 characters) before and after Jalandhar.
- Now you know between which two strings your location will be. Find the indexes of both those strings using IndexOf methods. Say these are x1 and x2 respectively.
- The required substring where location is mentioned can be find by using Substring method as Substring(x1,x2-x1).
// Getting IP Address
{
IPHostEntry ip = Dns.GetHostEntry(Dns.GetHostName());
IPAddress[] hst = ip.AddressList;
Label1.Text = hst[2].ToString();
string ip1=Label1.Text;
//get location
System.Net.WebClient wc = new System.Net.WebClient();
byte[] data = wc.DownloadData("http://www.geobytes.com/IpLocator.htm?GetLocation&IpAddress="+ip1);
string res = System.Text.Encoding.UTF8.GetString(data);
string strt="";
string end = "\" size=\"20\" readonly";
Label2.Text = getsubstring(res, strt, end);
}
public static string getsubstring(string text, string beg, string end)
{
int x1 = text.IndexOf(beg, 0) + beg.Length;
int x2 = text.IndexOf(end, x1);
return text.Substring(x1, x2-x1);
}