#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. Hashmap 详解和迭代器问题

    重点介绍HashMap.首先介绍一下什么是Map.在数组中我们是通过数组下标来对其内容索引的,而在Map中我们通过对象来对对象进行索引,用来索引的对象叫做key,其对应的对象叫做value.在下文中会 ...

  2. MVC HtmlHelper扩展——实现分页功能

    MVC HtmlHelper扩展类(PagingHelper) using System; using System.Collections.Generic; using System.Collect ...

  3. SSRS 报表 如何加参数

    SSRS 报表 如何加参数 连接上以后出现一个问题 就是给报表加上参数以后报表不断刷新,跟上次那个报表刷新是同样的问题.那么下面我们来解决一下. 1. 这是给报表添加默认参数进入页面后就不断的刷新刷新 ...

  4. js---通过arguments来获取指定参数

    通过访问arguments对象的length属性可以获取有多少个参数传递给了函数. 如:每次被调用的时候,输出传入其中的参数个数 function doAdd(){ alert(arguments.l ...

  5. jQuery基本选择器模块(二)

    选择器模块 1.push方法的兼容性(了解) 问题:IE8不支持aplly方法中的第二个参数是 伪数组 目标:实现 push 方法的浏览器兼容性问题 var push = [].push; try { ...

  6. Java学习-课堂总结

    一.字符串比较方式 1)‘==’   地址值比较      2) equals()方法   内容比较 二.String类的两种实例化方式     1)String str=“Hello”:     2 ...

  7. c++ 编译期与运行期

    分享到 一键分享 QQ空间 新浪微博 百度云收藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ好友 和讯微博 更多... 百度分享 转自:http://h ...

  8. swift pragma mark

    众所周知,大家在OC中对代码进行逻辑组织 用的是#pragma mark - ,生成分隔线 用#pragma mark 函数说明,来生成一个函数的说明X 但在swift中,这个语法就不支持了,毕竟它是 ...

  9. spring注解略解

    @Scope("prototype") 如果ACTION中不@Scope("prototype"),有可能报找不到XXXACTION的错误!写上这个就表示每次请 ...

  10. 添加图标:before 和 :after css中用法

    #sTitle:after{ position: absolute; top: 2px; font-family: "FontAwesome"; content: "\f ...