Simple Method for ASP. NET to obtain information such as IP address and computer name + common file source code

Source: Internet
Author: User
1. In ASP . NetPrivate attributes in:
Get server computer name: page. server. manchinename
Get user information: page. User
Obtain the client computer name: page. Request. userhostname
Obtain the Client Computer IP: Page. Request. userhostaddress

2. General methods in network programming:
Get the current computer name: static system. net. DNS. gethostname ()
Retrieve all data by computer nameIPAddress: static system. net. DNS. Resolve (computer name). Addresslist
You can alsoIPAddress retrieval computer name: static system. net. DNS. Resolve (IPAddress). hostname

3. General attributes of the system environment:
Current computer name: static system. environment. machinename
Domain Name of the current computer: static system. environment. userdomainname
Current computer user: static system. environment. Username

A public class that obtains the Intranet and Internet IP addresses of the local machine.

Using system;
Using system. IO;
Using system. net;
Using system. text;
Using system. Text. regularexpressions;
Namespace ipip
{
/// <Summary>
/// Summary of class1.
/// Obtain the local Internet IP address and Intranet IP Address
/// </Summary>
Public class class1
{
 
Private string strgetip;

Public class1 ()
{
Netip ();
Getip ();
}

Public String renetip ()
{Return netip () ;}// returns the network IP address.

Public String regetip ()
{Return strgetip;} // returns the Intranet IP address.


Static string netip ()
{
Uri uri = new uri ("http://www.ikaka.com/ip/index.asp"); // query the web page of the local network IP address
Httpwebrequest Req = (httpwebrequest) webrequest. Create (URI );
Req. method = "Post ";
Req. contenttype = "application/X-WWW-form-urlencoded ";
Req. contentlength = 0;
Req. cookiecontainer = new cookiecontainer ();
Req. getrequeststream (). Write (New byte [0], 0, 0 );
Httpwebresponse res = (httpwebresponse) (req. getresponse ());
Streamreader rs = new streamreader (res. getresponsestream (), encoding. getencoding ("gb18030 "));
String S = Rs. readtoend ();
Rs. Close ();
Req. Abort ();
Res. Close ();
Match m = RegEx. Match (S, @ "IP :\[(? <Ip> [0-9 \.] *) \] ");
If (M. Success) return M. Groups ["ip"]. value;
String strnetip = string. empty;
Return strnetip;
}

Public String getip () // note the difference with static
{
System. net. IPaddress [] Addresslist = DNS. gethostbyname (DNS. gethostname (). Addresslist; // obtain the Intranet IP address of the local machine.

Strgetip = Addresslist [0]. tostring ();
Return strgetip;

}
}
}

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.