Obtain the Internet IP address of the user's computer

Source: Internet
Author: User

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.