获取公网IP
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#include<wininet.h> #pragma comment(lib,"wininet.lib") DWORD HttpGet(LPCTSTR, char *, int); void GetPublicIp(char *pIP, int len) { char szBuffer[1024]; memset(szBuffer, 0, sizeof(char) * 1024); if (HttpGet(_TEXT("http://city.ip138.com/ip2city.asp"), szBuffer, 1024)) { char* begin = strstr(szBuffer, "["); char* end = strstr(begin, "]"); if (begin == NULL || end == NULL) return; int offset = end - begin - 1; begin[offset] = '\0'; char *ip = begin; strncpy_s(pIP, len, ip, offset); } } DWORD HttpGet(LPCTSTR lpszFullUrl, char *pBuffer, int iBufferSize) { if (lpszFullUrl == NULL) return 0; HINTERNET hNet = ::InternetOpen(_TEXT("Mozilla/5.0 (Windows NT 6.3; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0"), PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0); if (hNet == NULL) { return 0; } HINTERNET hUrlFile = ::InternetOpenUrl(hNet, lpszFullUrl, NULL, 0, INTERNET_FLAG_RELOAD, 0); if (!hUrlFile) { return 0; } ::memset(pBuffer, 0, iBufferSize); DWORD dwBytesRead = 0; char szTemp[1024] = { 0 }; DWORD dwTotalReadBytes = 0; while (InternetReadFile(hUrlFile, szTemp, sizeof(szTemp) - 1, &dwBytesRead)) { if (0 == dwBytesRead) break; szTemp[dwBytesRead] = 0; // 缓冲区大小不够时,停止向目的缓冲区复制数据,跳出 if (dwTotalReadBytes + dwBytesRead >= (UINT)iBufferSize) { pBuffer[iBufferSize - 1] = 0; break; } strcat_s(pBuffer, 1024, szTemp); ZeroMemory(szTemp, sizeof(szTemp)); dwTotalReadBytes += dwBytesRead; } ::InternetCloseHandle(hUrlFile); ::InternetCloseHandle(hNet); return dwTotalReadBytes; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=1033