# include <windows.h>
# include <urlmon.h>
# pragma comment(lib,"URLMON.lib")
void main()
{
URLDownloadToFile(NULL," http://www.ip138.com/ip2city.asp","ip.txt",0,NULL);
system("notepad ip.txt");
}
# include <urlmon.h>
# pragma comment(lib,"URLMON.lib")
void main()
{
URLDownloadToFile(NULL," http://www.ip138.com/ip2city.asp","ip.txt",0,NULL);
system("notepad ip.txt");
}
#include<urlmon.h>
#pragma comment (lib,"Urlmon.lib")
#pragma comment (lib,"Urlmon.lib")
char buf[MAX_PATH] = {0};
char chTempIp[128];
char chIP[64];
URLDownloadToFile(0," http://www.ip138.com/ip2city.asp","c:\\1.ini",0,NULL);
FILE *fp=fopen( "c:\\1.ini", "r" );
if ( fp != NULL )
{
fseek(fp, 0, SEEK_SET);
fread(buf,1,256,fp);
fclose(fp);
char* iIndex = strstr(buf,"[");
if (iIndex)
{
sprintf( chTempIp,"%s",iIndex);
int nBuflen = strlen(chTempIp);
for(int i =0; i<nBuflen; i++)
{
chIP[i] = chTempIp[i+1];
if(chTempIp[i] == ']')
{
chIP[i-1] = '\0';
DeleteFile("c:\\1.ini");
continue;
}
}
}
}
MessageBox(chIP);
if ( fp != NULL )
{
fseek(fp, 0, SEEK_SET);
fread(buf,1,256,fp);
fclose(fp);
char* iIndex = strstr(buf,"[");
if (iIndex)
{
sprintf( chTempIp,"%s",iIndex);
int nBuflen = strlen(chTempIp);
for(int i =0; i<nBuflen; i++)
{
chIP[i] = chTempIp[i+1];
if(chTempIp[i] == ']')
{
chIP[i-1] = '\0';
DeleteFile("c:\\1.ini");
continue;
}
}
}
}
MessageBox(chIP);
MFC////////////////////////////////////////////
#include <afxinet.h>
void CLanChatDlg::GetNetIP()
{
#include <afxinet.h>
void CLanChatDlg::GetNetIP()
{
SetDlgItemText(IDC_NET_IP,"正在获取外网IP");
CString strsource;
CString Address;
CInternetSession mySession(NULL,0);
CHttpFile* myHttpFile=NULL;
CString strsource;
CString Address;
CInternetSession mySession(NULL,0);
CHttpFile* myHttpFile=NULL;
Address="
http://www.ip138.com/ip2city.asp";//ip138网页
myHttpFile=(CHttpFile*)mySession.OpenURL(Address);//读取网络地址
while(myHttpFile->ReadString(strsource))
{ //循环读取下载来的网页文本
// AddToLog(strsource);
int begin=0;
begin=strsource.Find("[",0);
if(begin!=-1)//如果找到"[", 则找"]" 中括号内的文本则是 你的外网ip
{ int end=strsource.Find("]");
m_internetip=strsource.Mid(begin+1,end-begin-1);//提取外网ip
{ //循环读取下载来的网页文本
// AddToLog(strsource);
int begin=0;
begin=strsource.Find("[",0);
if(begin!=-1)//如果找到"[", 则找"]" 中括号内的文本则是 你的外网ip
{ int end=strsource.Find("]");
m_internetip=strsource.Mid(begin+1,end-begin-1);//提取外网ip
SetDlgItemText(IDC_NET_IP,m_internetip);//在左下角显示外网ip
}
}
}
}
本文介绍了两种方法来获取外网IP地址,分别使用C语言和MFC(Microsoft Foundation Classes)框架。C语言实现通过URLDownloadToFile函数下载网页内容,然后解析IP。MFC实现则利用CHttpFile类从网络读取数据,查找并显示外网IP。
1万+

被折叠的 条评论
为什么被折叠?



