C#獲取路由器外網(wǎng)IP,MAC地址的實現(xiàn)代碼
C#實現(xiàn)的獲取路由器MAC地址,路由器外網(wǎng)地址。對于要獲取路由器MAC地址,一定需要知道路由器web管理系統(tǒng)的用戶名和密碼。至于獲取路由器的外網(wǎng)IP地址,可以不需要知道路由器web管理系統(tǒng)的用戶名和密碼,但是需要有一個代理頁面獲取客戶端公網(wǎng)ip地址的,這樣C#請求此頁面即可獲取到路由器公網(wǎng)ip地址。如
//getip.ashx
測試路由為水星 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)默認編碼為gb2312
/// <summary>
/// 使用HttpWebRequest對象發(fā)送請求
/// </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)地址了,默認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)頁 http://www.ip138.com/ip2city.asp
myHttpFile=(CHttpFile*)mySession.OpenURL(csAddress);//讀取網(wǎng)絡(luò)地址
while(myHttpFile->ReadString(csSource))
{ //循環(huán)讀取下載來的網(wǎng)頁文本
//code 轉(zhuǎn)換
char *pStr = (char*)csSource.GetBuffer(csSource.GetLength()); //取得str對象的原始字符串
int nBufferSize = MultiByteToWideChar(CP_UTF8, 0,pStr, -1, NULL, 0); //取得所需緩存的多少
wchar_t *pBuffer = (wchar_t*)malloc(nBufferSize * sizeof(wchar_t));//申請緩存空間
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)//如果找到"[", 則找"]" 中括號內(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;
//為適配器申請內(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;
}
上一篇:C# winform 模擬鍵盤輸入自動接入訪問網(wǎng)絡(luò)的實例
欄 目:C#教程
下一篇:C#的Process類調(diào)用第三方插件實現(xiàn)PDF文件轉(zhuǎn)SWF文件
本文標題:C#獲取路由器外網(wǎng)IP,MAC地址的實現(xiàn)代碼
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6175.html
您可能感興趣的文章
- 01-10C#通過反射獲取當前工程中所有窗體并打開的方法
- 01-10C#獲取進程或線程相關(guān)信息的方法
- 01-10C#調(diào)用dos窗口獲取相關(guān)信息的方法
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10C#獲取任務(wù)欄顯示進程的方法
- 01-10C#及WPF獲取本機所有字體和顏色的方法
- 01-10C#獲取動態(tài)生成的CheckBox值
- 01-10C#獲取網(wǎng)頁源代碼的方法
- 01-10C#獲取客戶端相關(guān)信息實例總結(jié)
- 01-10C#實現(xiàn)獲取不同對象中名稱相同屬性的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改


