#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. python的模块导入

    单个文件导入:导入的模块可以是一个py文件(放置在当前文件的同级目录.默认路径等) 导入:import 模块名 使用:模块名.函数名 导入:from 模块名 import * 使用:函数名 ----- ...

  2. AJAX异步删除操作

    @Ajax.ActionLink("删除", "Delete", new {id = user.Id}, ajaxOption) @{ var ajaxOpti ...

  3. JUC集合-BlockingQueue

    BlockingQueue 阻塞队列,支持两个附加操作. 1,在队列为空时,获取元素的线程会等待对列变为非空. 2,在队列为满时,存储元素的线程会等待对列可用. 使用场景: 生产者往对列里添加元素 消 ...

  4. Python编程Web框架 :Django 从入门到精通

    Django是一个高级别的Python Web框架,它鼓励快速开发和干净实用的设计. 现在我们开始学习它. Django学习之 第一章:Django介绍 Django学习之 第二章:Django快速上 ...

  5. sql 查询多列 小于某值

    select COUNT(*) from ( select ID,H1 AS Value from Table_1 union all select ID,H2 from Table_1 union ...

  6. children ie8下获取错误

    ParentNode.children 是一个只读属性,返回 一个Node的子elements,是一个动态更新的 HTMLCollection. Internet Explorer 6 - 8 支持该 ...

  7. H3C三层交换机S5500初始配置+网络访问策略

    DHCP中继配置命令 dhcp relay address-check enable 命令用来使能DHCP 中继的地址匹配检查功能. undo dhcp relay address-check ena ...

  8. Surrogate data 代理数据

    附一篇science论文,待啃: 附Surrogate time series and fields,matlab:https://www.sogou.com/link?url=DSOYnZeCC_p ...

  9. JS 20180416作业

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. Python——Day2(基础知识练习一)

    1.执行Python脚本的两种方式1)调用解释器 Python +绝对路径+文件名称2)调用解释器 Python +相对路径+文件名称 2.简述位.字节的关系8位为1个字节 3.简述ASCII.uni ...