WebService obtains server hardware information and Client IP addresses, MAC addresses, and browser information.

Source: Internet
Author: User
Tags servervariables

WebService obtains the server hardware information and Client IP address, MAC address, and browser information, and the city in which the server resides --
 
 
 
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Diagnostics;
Using System. Web;
Using System. Web. Services;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Using System. Management;
Using System. Net;
Using System. Runtime. InteropServices;
Using System. IO;
Namespace CHardwardInfo
{
/// <Summary>
/// Summary of Service1.
/// GetHostName () Get the machine name
/// GetHostIP () obtains the Server IP Address
/// GetHostMac () Get the MAC address of the server
/// GetCustomerIP () obtains the dynamic IP address of the client.
/// GetCustomerMacInfo () obtain the MAC address of the Intranet Client
/// GetCustomerBroswer () client browser Information
/// GetCpuId (). Obtain the CPU ID
/// GetMainHardDiskId
/// To reference a public // Webservice: http://www.youthbar.com/IpSearch/GetAddressByIP.asmx
/// </Summary>

Public class Service1: System. Web. Services. WebService
{
[DllImport ("Iphlpapi. dll")]
Private static extern int SendARP (Int32 dest, Int32 host, ref Int64 mac, ref Int32 length );
[DllImport ("Ws2_32.dll")]
Private static extern Int32. Net_addr (string ip );
Public Service1 ()
{
// CODEGEN: This call is required by the ASP. Net Web service designer.
InitializeComponent ();
}

# Code generated by the region component designer

// Required by the Web Service designer
Private IContainer components = null;

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing & components! = Null)
{
Components. Dispose ();
}
Base. Dispose (disposing );
}

# Endregion

// WEB service example
// HelloWorld () Sample Service returns the string Hello World
// To generate a project, uncomment the following lines and save and generate the project
// To test this Web service, press F5

[WebMethod]
Public string GetHostName ()
{
Return System. Net. Dns. GetHostName ();
}
[WebMethod]
Public string GetCpuId ()
{
System. Management. ManagementClass mc = new ManagementClass ("Win32_Processor ");
System. Management. ManagementObjectCollection moc = mc. GetInstances ();
String strCpuID = null;
Foreach (System. Management. ManagementObject mo in moc)
{
StrCpuID = mo. Properties ["ProcessorID"]. Value. ToString ();
Break;
}
Return strCpuID;

}
[WebMethod]
Public string GetMainHardDiskId ()

{

ManagementObjectSearcher searcher = new ManagementObjectSearcher ("SELECT * FROM Win32_PhysicalMedia ");
String strHardDiskID = null;
Foreach (ManagementObject mo in searcher. Get ())
{
StrHardDiskID = mo ["SerialNumber"]. ToString (). Trim ();
Break;
}
Return strHardDiskID;

}
[WebMethod]
Public string GetHostIP ()
{
String s = "";
System. Net. IPAddress [] addressList = Dns. GetHostByName (Dns. GetHostName (). AddressList;
For (int I = 0; I <addressList. Length; I ++)
{
S + = addressList [I]. ToString ();
}
Return s;
}
[WebMethod]
Public string GetHostMAC ()
{
String mac = "";
System. Management. managementclass MC = new managementclass ("win32.networkadapterconfiguration ");
Managementobjectcollection MOC = mc. getinstances ();
Foreach (managementobject Mo in MoC)
{
If (Mo ["ipenabled"]. tostring () = "true ")
MAC = Mo ["macaddress"]. tostring ();
}
Return mac;
}
[WebMethod]
Public string GetCustomerIP ()
{
String CustomerIP = "";
If (HttpContext. Current. Request. ServerVariables ["HTTP_VIA"]! = Null)
{
CustomerIP = HttpContext. Current. Request. ServerVariables ["HTTP_X_FORWARDED_FOR"]. ToString ();

}
Else
{
CustomerIP = HttpContext. Current. Request. ServerVariables ["REMOTE_ADDR"]. ToString ();

}
Return CustomerIP;


}
[WebMethod]
Public string GetCustomerBroswer ()
{

Return HttpContext. Current. Request. ServerVariables ["HTTP_USER_AGENT"]. ToString ();
}
[WebMethod]
Public string GetCustomerMacInfo (string HostIP, string CustomerIP)
{

Int32 ldest =. Net_addr (CustomerIP); // ip address of the destination
Int32 lhost =. Net_addr (HostIP); // The local ip address.
Int64 macinfo = new Int64 ();
Int32 len = 6;
// Int len = macinfo. Length;
SendARP (ldest, 0, ref macinfo, ref len );
String originalMACAddress = Convert. ToString (macinfo, 16 );

If (originalMACAddress. Length <12)

{

OriginalMACAddress = originalMACAddress. PadLeft (12, '0 ');

}

String macAddress;

If (originalMACAddress! = "0000" & originalMACAddress. Length = 12)

{

String mac1, mac2, mac3, mac4, mac5, mac6;

Mac1 = originalMACAddress. Substring (10, 2 );

Mac2 = originalMACAddress. Substring (8, 2 );

Mac3 = originalMACAddress. Substring (6, 2 );

Mac4 = originalMACAddress. Substring (4, 2 );

Mac5 = originalMACAddress. Substring (2, 2 );

Mac6 = originalMACAddress. Substring (0, 2 );

MacAddress = mac1 + "-" + mac2 + "-" + mac3 + "-" + mac4 + "-" + mac5 + "-" + mac6;

}

Else

{

MacAddress = "";

}
Return macAddress. ToUpper ();
}
[WebMethod]
Public string GetAddressByIp (string userIP)
{
GetAddress. GetAddressByIP address = new getAddress. GetAddressByIP ();
Return address. getaddress (userip). tostring ();
}

}
}

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.