[Help]获取公网IP问题?
下面的代码用来获取公网IP,一般用户可以获取到。
但是通过代理上网的用户获取不到,提示网络缓存拒绝访问。
主要通过两种方式来实现:
一个是向WEB服务器模拟发包,解析返回的数据包;
另外一个方式是获取WEB页面的源代码,解析其中的IP地址。
访问的WEB页面是:
http://www.ip138.com/ip2city.asp
但是通过代理上网的用户获取不到,提示网络缓存拒绝访问。
主要通过两种方式来实现:
一个是向WEB服务器模拟发包,解析返回的数据包;
另外一个方式是获取WEB页面的源代码,解析其中的IP地址。
访问的WEB页面是:
http://www.ip138.com/ip2city.asp
// GetPubIPDlg.cpp : implementation file
#pragma comment(lib,"ws2_32.lib")
#include <afxinet.h> //加入CIneternetSession的引用
//写文本文件
BOOL wFile(CString filePath,CString fileContent)
{
CFile file;
file.Open(filePath,CFile::modeCreate|CFile::modeWrite);
file.Write(fileContent,fileContent.GetLength());
file.Close();
return TRUE;
}
//测试模拟发包
void CGetPubIPDlg::testSendData(char *sData,CString strFilePath)
{
SOCKADDR_IN saServer;
LPHOSTENT lphostent;
WSADATA wsadata;
SOCKET hsocket;
int nRet;
char* host_name="www.ip138.com";
CString strBottomLine;
CString strReplyContet;
//要写入文本文件的内容
CString logContent;
// 初始化套接字
if(WSAStartup(MAKEWORD(2,2),&wsadata))
{
AfxMessageBox("初始化SOCKET出错");
return;
}
lphostent=gethostbyname(host_name);
if(lphostent==NULL)
{
AfxMessageBox("本地电脑没有IP");
return;
}
hsocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
saServer.sin_family = AF_INET;
saServer.sin_port = htons(80); //默认端口
saServer.sin_addr = *((LPIN_ADDR)*lphostent->h_addr_list); //***本地IP地址***
// 利用SOCKET连接
nRet = connect(hsocket,(LPSOCKADDR)&saServer,sizeof(SOCKADDR_IN));
if(nRet == SOCKET_ERROR)
{
AfxMessageBox("建立连接时出错");
closesocket(hsocket);
return ;
}
//发送的数据包写文本文件
logContent.Insert(logContent.GetLength(),"发送的数据包:\r\n");
logContent.Insert(logContent.GetLength(),sData);
wFile(strFilePath,logContent);
//利用SOCKET发送 ******
nRet = send(hsocket,sData,strlen(sData),0);
if(nRet==SOCKET_ERROR)
{
AfxMessageBox("发送数据包时出错");
closesocket(hsocket);
}
Sleep(100);
char Dest[3000];
nRet=1;
CString strMsg;
// 接收返回数据包
nRet=recv(hsocket,(LPSTR)Dest,sizeof(Dest),0);
Dest[nRet] =0;
CString strPubIP; //公网IP地址
strMsg.Format("%s",Dest);
//收到的Socket数据包写文本文件
logContent.Insert(logContent.GetLength(),"收到的Socket数据包:\r\n");
logContent.Insert(logContent.GetLength(),strMsg);
wFile(strFilePath,logContent);
CString strIPHead = ":[";
CString strIPTail ="] ";
//解析公网IP地址
strMsg = strMsg.Right(strMsg.GetLength()-strMsg.Find(strIPHead)-3);
strPubIP = strMsg.Left(strMsg.GetLength()-strMsg.Find(strIPTail)-14);
strPubIP = "公网IP: "+strPubIP;
AfxMessageBox(strPubIP);
}
//方法一
void CGetPubIPDlg::OnButton1()
{
//请求的数据包
char* req1= "GET /ip2city.asp HTTP/1.1\r\n"
"Accept: */*\r\n"
"Accept-Language: zh-cn\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)\r\n"
"Host: www.ip138.com\r\n"
"Connection: Keep-Alive\r\n\r\n";
testSendData(req1,"收发的数据包-01.txt");
}
//方法二
void CGetPubIPDlg::OnButton2()
{
//请求的数据包
char* req2= "GET /ip2city.asp HTTP/1.1\r\n"
"Accept: */*\r\n"
"Accept-Language: zh-cn\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)\r\n"
"Host: www.ip138.com\r\n"
"Connection: Close\r\n\r\n";
testSendData(req2,"收发的数据包-02.txt");
}
//方法三
void CGetPubIPDlg::OnButton3()
{
//请求的数据包
char* req3= "GET /ip2city.asp HTTP/1.1\r\n"
"Accept: */*\r\n"
"Accept-Language: zh-cn\r\n"
//"Accept-Encoding: gzip, deflate\r\n"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)\r\n"
"Host: www.ip138.com\r\n"
"Connection: Keep-Alive\r\n\r\n";
testSendData(req3,"收发的数据包-03.txt");
}
//方法四
void CGetPubIPDlg::OnButton4()
{
//请求的数据包
char* req4= "GET /ip2city.asp HTTP/1.1\r\n"
"Accept: */*\r\n"
"Accept-Language: zh-cn\r\n"
//"Accept-Encoding: gzip, deflate\r\n"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)\r\n"
"Host: www.ip138.com\r\n"
"Connection: Close\r\n\r\n";
testSendData(req4,"收发的数据包-04.txt");
}
//方法五 获取远程页面的源文件
void CGetPubIPDlg::OnButton5()
{
CString m_SiteName ="http://www.ip138.com/ip2city.asp";
CString m_SiteInfo ="";
CInternetSession mySession(NULL,0);
CHttpFile* myHttpFile=NULL;
m_SiteInfo="连接到站点"+m_SiteName+"\r\n";
UpdateData(false);
CString myData;
myHttpFile=(CHttpFile*)mySession.OpenURL(m_SiteName);
while(myHttpFile->ReadString(myData))
{
m_SiteInfo=m_SiteInfo+"\r\n";
m_SiteInfo+=myData;
}
myHttpFile->Close ;
mySession.Close ;
//远程文件的源代码写文本文件
wFile("远程页面源文件.txt",m_SiteInfo);
//从源代码中解析出公网IP地址
CString strIPHead = ":[";
CString strIPTail ="] ";
//截取公网IP地址
CString strPubIP;
m_SiteInfo = m_SiteInfo.Right(m_SiteInfo.GetLength()-m_SiteInfo.Find(strIPHead)-3);
strPubIP = m_SiteInfo.Left(m_SiteInfo.GetLength()-m_SiteInfo.Find(strIPTail)-14);
//拼接公网IP和内网IP
strPubIP = "您的公网IP是:\r\n"+strPubIP;
AfxMessageBox(strPubIP);
}
...全文



