C#實現的獲取路由器MAC地址,路由器外網地址。對於要獲取路由器MAC地址,必定須要知道路由器web管理系統的用戶名和密碼。至於獲取路由器的外網IP地址,能夠不須要知道路由器web管理系統的用戶名和密碼,可是須要有一個代理頁面獲取客戶端公網ip地址的,這樣C#請求此頁面便可獲取到路由器公網ip地址。如:
http://xxxx.getip.ashx
測試路由爲水星 MR804,水星 MR808,均可以成功重啓路由和獲取到路由器MAC和外網IP地址html
源代碼下載地址:C#實現路由器重啓更換IP,獲取路由器MAC地址源代碼web
using System.Text; using System.Net; using System.Text.RegularExpressions; using System.IO; public class Router { Encoding gb2312 = Encoding.GetEncoding(936);//路由器的web管理系統默認編碼爲gb2312 /// <summary> /// 使用HttpWebRequest對象發送請求 /// </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和外網IP地址 /// </summary> /// <param name="RouterIP">路由IP地址,就是網關地址了,默認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); } }
出處:http://www.coding123.net/article/20120220/charp-get-router-wlan-ip-mac-address.aspx測試