MFC C++ 获取外网IP地址
生活随笔
收集整理的這篇文章主要介紹了
MFC C++ 获取外网IP地址
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <afxinet.h>//GB2312 轉換成 Unicode
wchar_t* GB2312ToUnicode(const char* szGBString)
{UINT nCodePage = 936; //GB2312int nLength=MultiByteToWideChar(nCodePage,0,szGBString,-1,NULL,0);wchar_t* pBuffer = new wchar_t[nLength+1];MultiByteToWideChar(nCodePage,0,szGBString,-1,pBuffer,nLength);pBuffer[nLength]=0;return pBuffer;
}CString CNetDlg::GetNetIp()
{CString internetIp = _T("");char chSource[4096] = {0};CString strAddress;CInternetSession mySession(NULL,0);CHttpFile* myHttpFile=NULL;strAddress = _T("http://20140507.ip138.com/ic.asp");//ip138網頁TRY{myHttpFile = (CHttpFile*)mySession.OpenURL(strAddress);//讀取網絡地址while(myHttpFile->Read(chSource, 4096)){//循環讀取下載來的網頁文本int begin = 0;// 目前只處理字符集為gb2312的情況begin = CKMP<BYTE*>::Find((BYTE*)chSource, strlen(chSource), (BYTE*)_C("charset=gb2312"), 14);if (begin != -1){WCHAR* wchSource = GB2312ToUnicode(chSource);internetIp = wchSource;delete[] wchSource;}begin = internetIp.Find(_T("["), 0);if(begin !=- 1)//如果找到"[", 則找"]"? 中括號內的文本則是 你的外網ip{ int end=internetIp.Find(_T("]"));internetIp = internetIp.Mid(begin+1, end-begin-1);//提取外網ipbreak;}}myHttpFile->Close();mySession.Close();}CATCH(CInternetException,e){internetIp.Format(T_T(_$_STRING_FORMAT_1), e->m_dwError, e->m_dwContext);}CATCH_ALL(e){TCHAR ch[MAX_PATH] = {0};e->GetErrorMessage(ch, MAX_PATH);internetIp.Format(T_T(_$_STRING_FORMAT_1), -3, ch);}END_CATCH_ALLTRACE(internetIp);return internetIp;
}
參考:
http://blog.csdn.net/cbk861110/article/details/7844729
網頁地址
http://20140507.ip138.com/ic.asp從
http://www.ip138.com的網頁源碼中查找到。
<tr><td align="center"><h3>www.ip138.com IP查詢(搜索IP地址的地理位置)</h3></td></tr><tr><td height="30" align="center" valign="top"><iframe src="http://20140507.ip138.com/ic.asp" rel="nofollow" frameborder="0" scrolling="no" width="100%" height="100%"></iframe></td></tr>總結
以上是生活随笔為你收集整理的MFC C++ 获取外网IP地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何生成CA证书
- 下一篇: VMProtect SDK+ASProt