设为首页 加入收藏

TOP

MFC C++ 获取外网IP地址
2015-07-20 18:05:32 来源: 作者: 【 】 浏览:3
Tags:MFC 获取 外网 地址
#include 
  
   

//GB2312 转换成 Unicode
wchar_t* GB2312ToUnicode(const char* szGBString)
{
    UINT nCodePage = 936; //GB2312

    int 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;
}

void CNetDlg::OnGetNetIp()
{
    // 本地化,以支持中文
    _tsetlocale(LC_ALL, _T("chs"));   // #include 
   
     CString strSource; char chSource[4096] = {0}; CString Address; CInternetSession mySession(NULL,0); CHttpFile* myHttpFile=NULL; Address=_T("http://20140507.ip138.com/ic.asp");//ip138网页 myHttpFile=(CHttpFile*)mySession.OpenURL(Address);//读取网络地址 try{ while(myHttpFile->Read(chSource, 4096)) { //循环读取
    下载来的网页文本 int begin=0; WCHAR* wchSource = GB2312ToUnicode(chSource); strSource = wchSource; ?? ??? ??? ?delete[] wchSource; OutputDebugString(strSource); begin=strSource.Find(_T("["), 0); if(begin!=-1)//如果找到"[", 则找"]" 中括号内的文本则是 你的外网ip { int end=strSource.Find(_T("]")); CString m_internetip=strSource.Mid(begin+1,end-begin-1);//提取外网ip AfxMessageBox(m_internetip); break; } } } catch(CInternetException *e) { AfxMessageBox(e->m_dwContext); } }
   
  


参考:

http://blog.csdn.net/cbk861110/article/details/7844729


网页地上从
http://www.ip138.com

的网页源码中查找到。

www.ip138.com IP查询(搜索IP地址的地理位置)

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj 3026 Borg Maze (bfs + 最小.. 下一篇CodeForces 449C Jzzhu and Apple..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: