C#獲取路由器外網(wǎng)IP,MAC地址的實(shí)現(xiàn)代碼
C#實(shí)現(xiàn)的獲取路由器MAC地址,路由器外網(wǎng)地址。對(duì)于要獲取路由器MAC地址,一定需要知道路由器web管理系統(tǒng)的用戶名和密碼。至于獲取路由器的外網(wǎng)IP地址,可以不需要知道路由器web管理系統(tǒng)的用戶名和密碼,但是需要有一個(gè)代理頁(yè)面獲取客戶端公網(wǎng)ip地址的,這樣C#請(qǐng)求此頁(yè)面即可獲取到路由器公網(wǎng)ip地址。如
//getip.ashx
測(cè)試路由為水星 MR804,水星 MR808,都可以成功重啟路由和獲取到路由器MAC和外網(wǎng)IP地址
源代碼
using System.Text; using System.Net; using System.Text.RegularExpressions; using System.IO; public class Router { Encoding gb2312 = Encoding.GetEncoding(936);//路由器的web管理系統(tǒng)默認(rèn)編碼為gb2312 /// <summary> /// 使用HttpWebRequest對(duì)象發(fā)送請(qǐng)求 /// </summary> /// <param name="url"></param> /// <param name="encoding">編碼</param> /// <param name="cache">憑證</param> /// <returns></returns> private static string SendRequest(string url, Encoding encoding,CredentialCache cache) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); if (cache != null) { request.PreAuthenticate = true; request.Credentials = cache; } string html = null; try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader srd = new StreamReader(response.GetResponseStream(), encoding); html = srd.ReadToEnd(); srd.Close(); response.Close(); } catch (Exception ex) { html = "FALSE" + ex.Message; } return html; } /// <summary> /// 獲取路由MAC和外網(wǎng)IP地址 /// </summary> /// <param name="RouterIP">路由IP地址,就是網(wǎng)關(guān)地址了,默認(rèn)192.168.1.1</param> /// <param name="UserName">用戶名</param> /// <param name="Passowrd">密碼</param> /// <returns></returns> private string LoadMACWanIP(string RouterIP,string UserName,string Passowrd) { CredentialCache cache = new CredentialCache(); string url = "http://" + RouterIP + "/userRpm/StatusRpm.htm"; cache.Add(new Uri(url), "Basic", new NetworkCredential(UserName, Passowrd)); return SendRequest(url, gb2312, cache); } }
MFC 獲取外網(wǎng)IP地址和MAC地址
ip地址獲?。?/strong>
CString GetSystemIp(void) { CString csSource; CString csAddress; CString csIPAddress; csIPAddress.Format(_T(" ")); CInternetSession mySession(NULL,0); CHttpFile* myHttpFile=NULL; csAddress=_T("http://iframe.ip138.com/ic.asp");//ip138網(wǎng)頁(yè) http://www.ip138.com/ip2city.asp myHttpFile=(CHttpFile*)mySession.OpenURL(csAddress);//讀取網(wǎng)絡(luò)地址 while(myHttpFile->ReadString(csSource)) { //循環(huán)讀取下載來(lái)的網(wǎng)頁(yè)文本 //code 轉(zhuǎn)換 char *pStr = (char*)csSource.GetBuffer(csSource.GetLength()); //取得str對(duì)象的原始字符串 int nBufferSize = MultiByteToWideChar(CP_UTF8, 0,pStr, -1, NULL, 0); //取得所需緩存的多少 wchar_t *pBuffer = (wchar_t*)malloc(nBufferSize * sizeof(wchar_t));//申請(qǐng)緩存空間 MultiByteToWideChar(CP_UTF8, 0, pStr, -1 , pBuffer, nBufferSize*sizeof(wchar_t));//轉(zhuǎn)碼 //MessageBox(pBuffer); //顯示 csSource.Format(_T("%s"),pBuffer); free(pBuffer); //釋放緩存 int begin=0; begin=csSource.Find(_T("["),0); if(begin!=-1)//如果找到"[", 則找"]" 中括號(hào)內(nèi)的文本則是 你的外網(wǎng)ip { int end=csSource.Find(_T("]")); csIPAddress.Format(_T("%s"),csSource.Mid(begin+1,end-begin-1));//提取外網(wǎng)ip return csIPAddress; } } return csIPAddress; }
MAC地址獲?。?/p>
CString GetMacAddress(void) { //CString strIP,strGateWay,strSubnetMask; CString strMac; strMac.Format(_T("")); u_char pMac[6]; PIP_ADAPTER_INFO adp = NULL; ULONG uLong=0; //為適配器申請(qǐng)內(nèi)存 ::GetAdaptersInfo(adp,&uLong); adp = (PIP_ADAPTER_INFO)::GlobalAlloc(GPTR,uLong); //取得本地適配器結(jié)構(gòu)信息 if(::GetAdaptersInfo(adp,&uLong) == ERROR_SUCCESS) { if(adp != NULL) { //strMacAdd.Format("%s",adp->Address); memcpy(pMac,adp->Address,6); strMac.Format(_T("%02X-%02X-%02X-%02X-%02X-%02X"),pMac[0],pMac[1],pMac[2],pMac[3],pMac[4],pMac[5]); //strGateWay.Format(_T("%s"),adp->GatewayList.IpAddress.String);// 網(wǎng)關(guān) //strIP.Format(_T("%s"),adp->IpAddressList.IpAddress.String);//IP //strSubnetMask.Format(_T("%s"),adp->IpAddressList.IpMask.String);//子網(wǎng)掩碼 //MessageBox(strMac); } } return strMac; }
相關(guān)文章
C#基于HttpWebRequest實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求的方法分析
這篇文章主要介紹了C#基于HttpWebRequest實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求的方法,結(jié)合實(shí)例形式分析了C#使用HttpWebRequest類與System.IO類實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-02-02C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音(多音字)功能詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音(支持多音字)的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02C#實(shí)現(xiàn)關(guān)閉子窗口而不釋放子窗口對(duì)象的方法
下面小編就為大家?guī)?lái)一篇C#實(shí)現(xiàn)關(guān)閉子窗口而不釋放子窗口對(duì)象的方法 。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01Unity實(shí)現(xiàn)簡(jiǎn)單手勢(shì)識(shí)別
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)單手勢(shì)識(shí)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07