In the development process we often encounter the need for IP address, used to record the user's last login time address, or sokect network programming, and so on, here are two ways:
1.
Public static String GetIP ()
{
return system.web. HttpContext. Current.Request.UserHostAddress;
}
2.
public static string Getaddressip ()
{
String strURL = "http://www.ip138.com/ip2city.asp"; Get the IP URL
Uri uri = new Uri (strURL);
WebRequest webreq = WebRequest.Create (URI);
Stream s = webreq. GetResponse (). GetResponseStream ();
StreamReader sr = new StreamReader (s, Encoding.default);
String all = Sr. ReadToEnd (); Read the data format returned by the website: Your IP address is: [x.x.x.x]
Int J = All. IndexOf ("[");
int k = all. IndexOf ("]");
String tempip = All. Substring (j + 1, k-j-1);
String ip = Tempip. Replace ("]", ""). Replace ("", ""). Replace ("<", ""). Replace ("/", ""); Remove Miscellaneous Find IP
return IP;
}
How C # obtains the native network IP address