VC获取外网IP

#include <afxinet.h>
#include <iostream>
using namespace std;
int main()
{
	CInternetSession    session(_T("cctry.com"));
	CHttpFile           *pFile  = NULL;
	CStdioFile          *ptFile = NULL;
	CString             strTemp = _T("");
	DWORD               dwStat  = 0;

	try
	{
		pFile = (CHttpFile *)session.OpenURL(_T("http://ip.dnsexit.com"));
		pFile->QueryInfoStatusCode(dwStat);
		if(dwStat == HTTP_STATUS_OK)
		{
			ptFile = (CStdioFile *)pFile;
			cout<<_T("GetVerb   : ")<<pFile->GetVerb()<<endl;
			cout<<_T("GetObject : ")<<pFile->GetObject()<<endl;
			cout<<_T("GetFileURL: ")<<pFile->GetFileURL()<<endl;

			while(ptFile->ReadString(strTemp))
				cout<<_T("serverIP  : ")<<strTemp<<endl;

			pFile->Close();
			session.Close();
			return 0;
		}
	}
	catch(CException* e)
	{
		cout<<_T("open url failed")<<endl;
		return -1;
	}

	return -1;
}

VC获取外网IP_第1张图片


#include <iostream>
#include <string>
#include <cstdlib>
#include <afxinet.h>
using namespace std;

CString GetHtml(CString url)
{
	CString content;
	CString data;
	DWORD dwStatusCode;
	CInternetSession session("HttpClient");

	CHttpFile* pfile = (CHttpFile *)session.OpenURL(url);
	pfile -> QueryInfoStatusCode(dwStatusCode);
	if(dwStatusCode == HTTP_STATUS_OK)
	{ 
		while (pfile -> ReadString(data))
		{
			content  += data;
		}
	}
	pfile -> Close();
	delete pfile;
	session.Close();
	return content;
}

int main() 
{ 
	cout << GetHtml("http://ip.dnsexit.com/index.php") << endl;
	return 0;
}  


你可能感兴趣的:(VC获取外网IP)