#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>

MFC C++ 获取外网IP地址的更多相关文章

  1. c#获取外网IP地址的方法

    1.如果你是通过路由上网的,可以通过访问ip138之类的地址来获取外网IP 2.如果是通过PPPOE拨号上网的,可以使用以下代码获取IP //获取宽带连接(PPPOE拨号)的IP地址,timeout超 ...

  2. java获取外网ip地址

    转自:http://blog.163.com/houjunchang_daxue/blog/static/13037938320134543310451/ /** * 获取外网IP.归属地.操作系统 ...

  3. C# 获取外网IP地址

    很多情况下我们需要获取外网的IP地址,一般用自带的方法获取到的都是不准确,往往获取到的是内网的IP地址,所以需要采用外部网站接口来获取. 代码 通过访问第三方接口来获取真实的ip地址 public s ...

  4. linux 获取外网ip地址

    curl ifconfig.me 私有ip地址,获取公网ip

  5. android 根据网络来获取外网ip地址及国家,地区的接口

    新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...

  6. C# Winform程序获取外网IP地址

    string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了 Uri uri = new Uri(strUrl); ...

  7. 获取外网IP地址

    public static string GetRealIP(){            string result = String.Empty;            result = HttpC ...

  8. C#获取外网IP地址;C#获取所在IP城市地址

    public static string GetIP()         {             using (var webClient = new WebClient())           ...

  9. C#获取外网IP、本机MAC地址及Ping的实现

    原文 获取外网IP, C#获取本机的MAC地址,C#通过编程方式实现Ping 获取外网IP地址 思路是通过WebRequest连接一些网上提供IP查询服务的网站,下载到含有你的IP的网页,然后用正则表 ...

随机推荐

  1. GStreamer基础教程02 - 基本概念

    摘要 在 Gstreamer基础教程01 - Hello World中,我们介绍了如何快速的通过一个字符串创建一个简单的pipeline.为了能够更好的控制pipline中的element,我们需要单 ...

  2. Cracking the Coding Interview 5.2

    Given a(decimal -e.g. 3.72)number that is passed in as a string, print the binary representation. If ...

  3. 9.19[XJOI] NOIP训练37

    上午[XJOI] NOIP训练37 T1 同余方程 Problem description 已知一个整数a,素数p,求解 $x^{2}\equiv a(mod p) $ 是否有整数解 Solution ...

  4. java使用FileUtils文件操作神器

    前言: 在工作当中我们往往遇到很多文件的操作,我们也习惯写一些自己定义的工具类来简化文件操作,其实apache的commons的FileUtils类就是这样一个工具类,使用它能大大的简化我们对文件的操 ...

  5. 开发辅助 | 阿里图标库iconfont入门使用

    目前大多数的互联网公司,前端开发和UI设计师配合中,针对设计师给图的效果图,前端开发工程师不再像往常一样对于细小图标进行切图,取而代之的是引用阿里图标库(http://iconfont.cn/):简单 ...

  6. css 画三角形

    <div class='triangle-rihgt'></div> <div class='triangle-top'></div> <div ...

  7. 利用JavaScript的%做隔行换色

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  8. mybatis 高级映射和spring整合之查询缓存(5)

    mybatis 高级映射和spring整合之查询缓存(5) 2.0 查询缓存 2.0.1 什么是查询缓存 mybatis提供缓存,用于减轻数据压力,提高数据库性能. mybatis提供一级缓存和二级缓 ...

  9. 2nd

    Java语言基础(常量的概述和使用) A:什么是常量 在程序执行的过程中其值不可以发生改变 B:Java中常量的分类 字面值常量 自定义常量(面向对象部分讲) C:字面值常量的分类 字符串常量 用双引 ...

  10. VHDL之conversion function

    VHDL Type Cast and Conversion Functions **In ASIC design, do NEVER use integer or natural for signal ...