#include <afxinet.h>

//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;
} 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);//提取外网ip                 break;
            }
        }         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_ALL     TRACE(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>

最新文章

  1. publishing failed with multiple errors resource is out of sync with the file system--转
  2. 【Unity3d游戏开发】UGUI插件入门之游戏菜单
  3. linux杀死jobs的正确方法
  4. NSBundle控件和UIImageView和UIButton区别
  5. 简单验证码识别(matlab)
  6. C++中四种转换类型的区别
  7. Linux实验二报告
  8. ini 文件操作记要(1): 使用 TIniFile
  9. installing a 3D printer
  10. 算法--数组中出现一次的数,其余都出现N次
  11. 用 CSS 实现字符串截断
  12. MYSQL数据库性能调优之七:其他(读写分离、分表等)
  13. 【技术贴】解决Eclipse中SVN图标不显示
  14. 2015第24周三Spring事务3
  15. fatal error: malformed or corrupted AST file: &amp;#39;Unable to load module &amp;quot;/Users/apple/Library/Developer
  16. 基于jsoup的Java服务端http(s)代理程序-代理服务器Demo
  17. css3模拟jq点击事件
  18. EFcore与动态模型(二)
  19. codevs 3342 绿色通道
  20. mac与windows共享键盘鼠标(synergy)

热门文章

  1. [源码管理] Microsoft Visual SourceSafe 2005 下载与配置
  2. Python+unittest 接口自动化测试
  3. Mac OS X10.9安装的Python2.7升级Python3.4步骤详解
  4. BZOJ4518: [Sdoi2016]征途(dp+斜率优化)
  5. vue路由history模式下打包node服务器配置
  6. GoogleMap 获取自己的数字证书API key的步骤
  7. MySQL 5.6 Reference Manual-14.4 InnoDB Configuration
  8. Oracle中的SAVEPOINT
  9. poj 2955 Brackets 【 区间dp 】
  10. (转)RabbitMQ学习之路由(java)