MFC C++ 获取外网IP地址
#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地址的更多相关文章
- c#获取外网IP地址的方法
1.如果你是通过路由上网的,可以通过访问ip138之类的地址来获取外网IP 2.如果是通过PPPOE拨号上网的,可以使用以下代码获取IP //获取宽带连接(PPPOE拨号)的IP地址,timeout超 ...
- java获取外网ip地址
转自:http://blog.163.com/houjunchang_daxue/blog/static/13037938320134543310451/ /** * 获取外网IP.归属地.操作系统 ...
- C# 获取外网IP地址
很多情况下我们需要获取外网的IP地址,一般用自带的方法获取到的都是不准确,往往获取到的是内网的IP地址,所以需要采用外部网站接口来获取. 代码 通过访问第三方接口来获取真实的ip地址 public s ...
- linux 获取外网ip地址
curl ifconfig.me 私有ip地址,获取公网ip
- android 根据网络来获取外网ip地址及国家,地区的接口
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...
- C# Winform程序获取外网IP地址
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了 Uri uri = new Uri(strUrl); ...
- 获取外网IP地址
public static string GetRealIP(){ string result = String.Empty; result = HttpC ...
- C#获取外网IP地址;C#获取所在IP城市地址
public static string GetIP() { using (var webClient = new WebClient()) ...
- C#获取外网IP、本机MAC地址及Ping的实现
原文 获取外网IP, C#获取本机的MAC地址,C#通过编程方式实现Ping 获取外网IP地址 思路是通过WebRequest连接一些网上提供IP查询服务的网站,下载到含有你的IP的网页,然后用正则表 ...
随机推荐
- 使用ssh和putty操控远程的linux server
windows下没有openssh,今天这里使用openssh-server作为server,windows下使用putty作为client, putty主要流程分以下几步: step 1: 下载pu ...
- C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- HDFS与java API应用
java代码操作hadoop文件需要用hadoop的jar包,comment,hdfs,yarn,mapreduce,内均有有关jar包,eclipse操作hadoop还需要配置core-site.x ...
- 【MFC】模态、非模态对话框
MFC 点击按钮,弹出另一个对话框 方法一:模态对话框 资源视图–Dialog–右键–添加资源–新建–对话框-,然后在已经生成的对话框中(解决资源视图中的dialog下的新生成的那个)右键–添加类.例 ...
- Java中将JSON格式的数据转换成对应的Bean、Map、List数据
简单说明: 为了方便数据在客户端及服务器端的传输,有时候我们会用一些比较方便组织的数据类型,比如json.xml等传给客户端,客户端也可以重新组织数据传回服务器端.JSON和XML提供了一套比较方便的 ...
- SVN添加分支
1.打开版本库浏览视图 2.复制当前版本 3.输入复制的目的目录即可
- MySQL结构相关
MySQL 由以下几部分组成: 1.Connectors指的是不同语言中与SQL的交互 2.Management Serveices & Utilities: 系统管理和控制工具 3.Conn ...
- 02--C编程细节整理(一)
用C语言比较多,这篇是平时攒下的.有些内容在工作后可能会很常见,但是不用容易忘,所以就写篇博客吧. 1. printf的用法 %*可以用来跳过字符,可以用于未知缩进.像下面一样. for ...
- -webkit-appearance: none; 去除浏览器默认样式
-webkit-appearance: none; 去除浏览器默认样式
- 企业级任务调度框架Quartz(1) --企业应用中的任务调度介绍
由于目前的工作内容为建行CLPM批处理业务的设计工作,所以很好的理解批处理所用的任务调度框架Quartz势在必行:为了能够更好的去服务于工作,也 为了提升自己,所以我学习了Quartz Job Sch ...