/************************************************************************/
/* 功能:根据IP地址获取机器所在地区 */
/* 说明:此程序需要在联网状态下运行 */
/* 作者:杨帆 */
/* 日期:2010-2-26 */
/* 联系方式:QQ:522419441 */
/* E-mail:appview@163.com */
/************************************************************************/
#include <stdio.h>
#include <afxinet.h>
void Ip2Area(CString searchIP);
int main(int argc, char* argv[])
{
if (argc!=2)
{
printf("usage:\n\tip2area ip\nathor:\n\tyangfan\n");
return 0;
}
Ip2Area(argv[1]);
return 0;
}
void Ip2Area(CString searchIP)
{
CHttpFile *pHttpFile = NULL;
CInternetSession session("My Agent", 0);
CString strURL = "";
strURL.Format("%s?ip=%s&action=2","http://www.ip138.com/ips8.asp", searchIP);
pHttpFile = (CHttpFile*)session.OpenURL(strURL);
DWORD statusCode = 0;
BOOL bRet = FALSE;
bRet =pHttpFile->QueryInfoStatusCode(statusCode);
if (!(bRet && statusCode>=200 && statusCode<=299))
{
printf("Error Code:%d",statusCode);
return;
}
CString data;
char szBuffer[1024*16] = {0};
while((pHttpFile->Read(szBuffer,sizeof(szBuffer))>0))
{
data+=szBuffer;
memset(szBuffer,0,sizeof(szBuffer));
}
int iFind = data.Find("无效的IP地址", 0);
if (iFind!=-1)
{
printf("无效的IP地址\n");
return;
}
iFind = data.Find("本站主数据:", 0);
data = data.Mid(iFind+strlen("本站主数据:"));
iFind = data.Find("<", 0);
data.SetAt(iFind, '\0');
iFind = data.Find(" ", 0);
if (iFind!=-1)
{
data.SetAt(iFind, '\0');
}
printf("%s\n",data);
}
评论7
最新资源