[Cpp]
# Include "stdafx. h"
BOOL GetPubIp (char * ip );
Int main ()
{
Char myip [32] = {0 };
GetPubIp (myip );
Cout <myip <endl;
Return 0;
}
BOOL GetPubIp (char * ip)
{
HINTERNET hInternet = NULL;
HINTERNET hConnect = NULL;
Char szText [512] = {0 };
Char * szAccept = "Accept: */* \ r \ n ";
Char * szUrl = "http://www.ip138.com/ip2city.asp ";
DWORD dwReadLen = 0;
_ Try
{
HInternet = InternetOpen ("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)", 0, NULL, NULL, 0 );
If (! HInternet)
_ Leave;
HConnect = InternetOpenUrl (hInternet, szUrl, szAccept, strlen (szAccept), INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0 );
If (! HConnect)
_ Leave;
InternetReadFile (hConnect, szText, 512, & dwReadLen );
}
_ Finally
{
If (hInternet)
InternetCloseHandle (hInternet );
If (hConnect)
InternetCloseHandle (hConnect );
}
Char * start = strstr (szText, "your IP address is :[");
If (! Start)
Return FALSE;
Start = start + 15;
Char * temp = strstr (start, "]");
Strncpy (ip, start, temp-start );
Return TRUE;
}
From sniper_bing's column