There are many ways to obtain the user's Internet IP address in projects. The following two methods are described.
/// <Summary>
/// Obtain the IP address of the local machine in the LAN
/// </Summary>
/// <Returns> </returns>
Private string getlocalipaddress ()
{
System. net. IPaddress [] Addresslist = DNS. gethostentry (DNS. gethostname (). Addresslist;
String strnativeip = "";
String strserverip = "";
If (Addresslist. length> 1)
{
Strnativeip = Addresslist [0]. tostring ();
Strserverip = Addresslist [1]. tostring ();
}
Else if (Addresslist. Length = 1)
{
Strserverip = Addresslist [0]. tostring ();
}
Return strserverip;
}
The other is to capture the IP address of the Internet address queried on the web page. The implementation is as follows:
/// <Summary>
/// Obtain the IP address of the Local Machine
/// </Summary>
/// <Returns> </returns>
Private string getconnectnetaddress ()
{
String strurl = "http://www.ip138.com/ip2city.asp"; // obtain the IP Address 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]
Int I = all. indexof ("[") + 1;
String tempip = all. substring (I, 15 );
String IP = tempip. Replace ("]", ""). Replace ("", ""). Replace ("<","");
Return IP;
}