18,359
社区成员




void MyDbgPrintf(const char* pszFormat, ...)
{
//#ifdef _DEBUG
char szBufFormat[0x1000];
char szBufFormat_Game[0x1008] = "GameData:";
va_list argList;
va_start(argList, pszFormat);//参数列表初始化
vsprintf_s(szBufFormat, pszFormat, argList);
strcat_s(szBufFormat_Game, szBufFormat);
OutputDebugString(szBufFormat_Game);
va_end(argList);
//#endif
}
#include <afxinet.h>
BOOL Test(LPCTSTR strURL, CString &szRecv)
{
BOOL bRet = FALSE;
szRecv.Empty();
CInternetSession intsess;
CHttpConnection *pHttpCon = NULL;
CHttpFile *pHttpFile = NULL;
DWORD dwSvrType = AFX_INET_SERVICE_UNK;
CString strServer, strObject;
INTERNET_PORT nPort = INTERNET_DEFAULT_HTTP_PORT;
CString strUsername, strPassword;
if (AfxParseURLEx(strURL, dwSvrType, strServer, strObject, nPort, strUsername, strPassword, 0)
&& dwSvrType == AFX_INET_SERVICE_HTTP)
{
try
{
//连接服务器
pHttpCon = intsess.GetHttpConnection(strServer, nPort, strUsername, strPassword);
//发送请求
pHttpFile = pHttpCon->OpenRequest(NULL, strObject);
pHttpFile->SendRequest();
//状态200
DWORD dwStatCode = 0;
if (pHttpFile->QueryInfoStatusCode(dwStatCode)
&& dwStatCode == HTTP_STATUS_OK)
{
CStringA sDataA;
//读入数据
while (1)
{
CHAR cData[1024 + 1];
UINT uRd = pHttpFile->Read(cData, 1024);
if (uRd == 0)
break;
cData[uRd] = 0;
sDataA += cData;
}
//输出接收内容
OutputDebugStringA(sDataA);
OutputDebugStringA("\r\n");
#if (defined _UNICODE || defined UNICODE)
//多字节转换到UNICODE
int iWLen = MultiByteToWideChar(936, 0, sDataA, -1, NULL, 0);
MultiByteToWideChar(936, 0, sDataA, -1, szRecv.GetBuffer(iWLen+1), iWLen);
#else //!UNICODE
szRecv = sDataA;
#endif //UNICODE
bRet = TRUE;
}
}
catch (CInternetException* e)
{
e->ReportError();
e->Delete();
}
}
if (pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}
if (pHttpCon)
{
delete pHttpCon;
pHttpCon = NULL;
}
intsess.Close();
return bRet;
}
{
setlocale(LC_ALL, "chs");
CString szRecv;
if(Test(_T("http://whois.pconline.com.cn/ipJson.jsp?ip=182.84.29.37"), szRecv))
{
TRACE(_T("%s\n"), (LPCTSTR)szRecv);
}
}
// CDialog_Tab1.cpp: 实现文件
#include <afxinet.h>
#include <locale.h>
void CDialog_Tab1::OnBnClickedBtnShowIpPosition()
{
// TODO: 在此添加控件通知处理程序代码
for (int index = 0; index < m_listRoomPlayer.GetItemCount(); index++)
{
char szIP[20] = { 0 };
m_listRoomPlayer.GetItemText(index, 3, szIP, sizeof(szIP));
if (strlen(szIP) != 0)
{
char szUrl[MAX_PATH] = { 0 };
sprintf_s(szUrl, "http://whois.pconline.com.cn/ipJson.jsp?ip=%s", szIP);
MyDbgPrintf("index:%d,szUrl:%s", index, szUrl);
setlocale(LC_ALL, "chs");
g_cMyRoom.GetRoomPlayerIPPosition(index, szUrl);
}
}
}
//CRoom.cpp:
#include <afxinet.h>
#include <locale.h>
BOOL CRoom::GetRoomPlayerIPPosition(DWORD dwIndex, char * szURL)
{
CInternetSession intsess;
CHttpConnection *pHttpCon = NULL;
CHttpFile *pHttpFile = NULL;
DWORD dwSvrType = AFX_INET_SERVICE_UNK;
CString strServer, strObject;
INTERNET_PORT nPort = INTERNET_DEFAULT_HTTP_PORT;
CString strUsername, strPassword;
if (AfxParseURLEx(szURL, dwSvrType, strServer, strObject, nPort, strUsername, strPassword, 0)
&& dwSvrType == AFX_INET_SERVICE_HTTP)
{
try
{
//连接服务器
pHttpCon = intsess.GetHttpConnection(strServer, nPort, strUsername, strPassword);
MyDbgPrintf("pHttpCon:%x", pHttpCon);
//发送请求
pHttpFile = pHttpCon->OpenRequest(NULL, strObject);
MyDbgPrintf("pHttpFile:%x", pHttpFile);
if (pHttpFile->SendRequest() == FALSE)
{
MyDbgPrintf("pHttpFile->SendRequest() == FALSE");
return FALSE;
}
//状态200
DWORD dwStatCode = 0;
if (pHttpFile->QueryInfoStatusCode(dwStatCode)
&& dwStatCode == HTTP_STATUS_OK)
{
CStringA sDataA;
//读入数据
while (1)
{
CHAR cData[1024 + 1];
UINT uRd = pHttpFile->Read(cData, 1024);
if (uRd == 0)
break;
cData[uRd] = 0;
sDataA += cData;
}
//输出接收内容
MyDbgPrintf("%s", (LPCTSTR)CString(sDataA));
MyDbgPrintf("%s", sDataA.GetBuffer());
/*MyDialog.m_DlgTab1.m_listRoomPlayer.SetItemText(dwIndex, 4, (LPCTSTR)CString(sDataA));*/
}
}
catch (CInternetException* e)
{
e->ReportError();
e->Delete();
}
}
if (pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}
if (pHttpCon)
{
delete pHttpCon;
pHttpCon = NULL;
}
intsess.Close();
return TRUE;
}
Test(_T("http://whois.pconline.com.cn/ipJson.jsp?ip=59.54.49.72"))
我的调试输出
if(window.IPCallBack) {IPCallBack({"ip":"59.54.49.72","pro":"江西省","proCode":"360000","city":"九江市","cityCode":"360400","region":"","regionCode":"0","addr":"江西省九江市 电信","regionNames":"","err":""});}
BOOL CRoom::GetRoomPlayerIPPosition(DWORD dwIndex, char * szURL)
{
CInternetSession intsess;
CHttpConnection *pHttpCon = NULL;
CHttpFile *pHttpFile = NULL;
DWORD dwSvrType = AFX_INET_SERVICE_UNK;
CString strServer, strObject;
INTERNET_PORT nPort = INTERNET_DEFAULT_HTTP_PORT;
CString strUsername, strPassword;
if (AfxParseURLEx(szURL, dwSvrType, strServer, strObject, nPort, strUsername, strPassword, 0)
&& dwSvrType == AFX_INET_SERVICE_HTTP)
{
try
{
//连接服务器
pHttpCon = intsess.GetHttpConnection(strServer, nPort, strUsername, strPassword);
//发送请求
pHttpFile = pHttpCon->OpenRequest(NULL, strObject);
pHttpFile->SendRequest();
//状态200
DWORD dwStatCode = 0;
if (pHttpFile->QueryInfoStatusCode(dwStatCode)
&& dwStatCode == HTTP_STATUS_OK)
{
CStringA sDataA;
//读入数据
while (1)
{
CHAR cData[1024 + 1];
UINT uRd = pHttpFile->Read(cData, 1024);
if (uRd == 0)
break;
cData[uRd] = 0;
sDataA += cData;
}
//输出接收内容
MyDbgPrintf("%s", (LPCTSTR)CString(sDataA));
MyDbgPrintf("%s", sDataA.GetBuffer());
/*MyDialog.m_DlgTab1.m_listRoomPlayer.SetItemText(dwIndex, 4, (LPCTSTR)CString(sDataA));*/
}
}
catch (CInternetException* e)
{
e->ReportError();
e->Delete();
}
}
if (pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}
if (pHttpCon)
{
delete pHttpCon;
pHttpCon = NULL;
}
intsess.Close();
return TRUE;
}
参数应该没问题 主要是前辈这段代码我看不懂..得麻烦前辈帮帮了!void CDialog_Tab1::OnBnClickedBtnShowIpPosition()
{
// TODO: 在此添加控件通知处理程序代码
for (int index = 0; index < m_listRoomPlayer.GetItemCount(); index++)
{
char szIP[20] = { 0 };
m_listRoomPlayer.GetItemText(index, 3, szIP, sizeof(szIP));
MyDbgPrintf("strlen(szIP):%u", strlen(szIP));
if (strlen(szIP) != 0)
{
char szUrl[MAX_PATH] = { 0 };
//sprintf_s(szUrl, "http://whois.pconline.com.cn/ipJson.jsp?ip=%s", szIP);
sprintf_s(szUrl, "http://whois.pconline.com.cn/ipJson.jsp?json=true");
setlocale(LC_ALL, "chs");
g_cMyRoom.GetRoomPlayerIPPosition(index, szUrl);
}
}
}
BOOL CRoom::GetRoomPlayerIPPosition(DWORD dwIndex, char * szURL)
{
CInternetSession intsess;
CHttpConnection *pHttpCon = NULL;
CHttpFile *pHttpFile = NULL;
DWORD dwSvrType = AFX_INET_SERVICE_UNK;
CString strServer, strObject;
INTERNET_PORT nPort = INTERNET_DEFAULT_HTTP_PORT;
CString strUsername, strPassword;
if (AfxParseURLEx(szURL, dwSvrType, strServer, strObject, nPort, strUsername, strPassword, 0)
&& dwSvrType == AFX_INET_SERVICE_HTTP)
{
try
{
//连接服务器
pHttpCon = intsess.GetHttpConnection(strServer, nPort, strUsername, strPassword);
//发送请求
pHttpFile = pHttpCon->OpenRequest(NULL, strObject);
pHttpFile->SendRequest();
//状态200
DWORD dwStatCode = 0;
if (pHttpFile->QueryInfoStatusCode(dwStatCode)
&& dwStatCode == HTTP_STATUS_OK)
{
CStringA sDataA;
//读入数据
while (1)
{
CHAR cData[1024 + 1];
UINT uRd = pHttpFile->Read(cData, 1024);
if (uRd == 0)
break;
cData[uRd] = 0;
sDataA += cData;
}
//输出接收内容
MyDbgPrintf("%s", (LPCTSTR)CString(sDataA));
MyDbgPrintf("%s", sDataA.GetBuffer());
/*MyDialog.m_DlgTab1.m_listRoomPlayer.SetItemText(dwIndex, 4, (LPCTSTR)CString(sDataA));*/
}
}
catch (CInternetException* e)
{
e->ReportError();
e->Delete();
}
}
if (pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}
if (pHttpCon)
{
delete pHttpCon;
pHttpCon = NULL;
}
intsess.Close();
return TRUE;
}
#include <afxinet.h>
void Test(LPCTSTR strURL = _T("http://whois.pconline.com.cn/ipJson.jsp?json=true"))
{
CInternetSession intsess;
CHttpConnection *pHttpCon = NULL;
CHttpFile *pHttpFile = NULL;
DWORD dwSvrType = AFX_INET_SERVICE_UNK;
CString strServer, strObject;
INTERNET_PORT nPort = INTERNET_DEFAULT_HTTP_PORT;
CString strUsername, strPassword;
if(AfxParseURLEx(strURL, dwSvrType, strServer, strObject, nPort, strUsername, strPassword, 0)
&& dwSvrType == AFX_INET_SERVICE_HTTP )
{
try
{
//连接服务器
pHttpCon = intsess.GetHttpConnection(strServer, nPort, strUsername, strPassword);
//发送请求
pHttpFile = pHttpCon->OpenRequest(NULL, strObject);
pHttpFile->SendRequest();
//状态200
DWORD dwStatCode = 0;
if(pHttpFile->QueryInfoStatusCode(dwStatCode)
&& dwStatCode == HTTP_STATUS_OK)
{
CStringA sDataA;
//读入数据
while(1)
{
CHAR cData[1024 + 1];
UINT uRd = pHttpFile->Read(cData, 1024);
if(uRd == 0)
break;
cData[uRd] = 0;
sDataA += cData;
}
//输出接收内容
TRACE(_T("%s\n"), (LPCTSTR)CString(sDataA));
}
}
catch (CInternetException* e)
{
e->ReportError();
e->Delete();
}
}
if(pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
pHttpFile = NULL;
}
if(pHttpCon)
{
delete pHttpCon;
pHttpCon = NULL;
}
intsess.Close();
}
#include <locale.h>
//测试用例
{
setlocale(LC_ALL, "chs");
Test();
}