核心代码:
#include <wininet.h>
#pragma comment(lib,"wininet.lib")
BOOL GetHttpContext(LPCTSTR lpszURL,CString & strData)
{
HINTERNET hInternetOpen = NULL,hInternetUrl=NULL;
BOOL bResult = FALSE;
try{
hInternetOpen = InternetOpen(TEXT("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,NULL);
if(hInternetOpen == NULL)
throw "InternetOpen is null!";
//设置连接超时
DWORD dwTimeOut = 10 * 1000;
InternetSetOption(hInternetOpen,INTERNET_OPTION_CONNECT_TIMEOUT , (LPVOID)&dwTimeOut,sizeof(DWORD));
//设置接收超时
dwTimeOut = 10 * 1000;
InternetSetOption(hInternetOpen,INTERNET_OPTION_RECEIVE_TIMEOUT , (LPVOID)&dwTimeOut,sizeof(DWORD));
//设置发送超时
dwTimeOut = 10 * 1000;
InternetSetOption(hInternetOpen,INTERNET_OPTION_SEND_TIMEOUT , (LPVOID)&dwTimeOut,sizeof(DWORD));
hInternetUrl = InternetOpenUrl(hInternetOpen,lpszURL,_T("Accept: */*"),-1,INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE,NULL);
if(hInternetUrl == NULL)
throw "hInternetUrl is null!";
BYTE byteBuffer[1024];
DWORD dwReadByteSize = 0;
BOOL bReadResult = FALSE;
while(TRUE)
{
dwReadByteSize=0;
ZeroMemory(byteBuffer,sizeof(byteBuffer));
bReadResult = InternetReadFile(hInternetUrl,byteBuffer,sizeof(byteBuffer ) - 1,&dwReadByteSize);
//检测数据是否读完了
if(bReadResult == TRUE && dwReadByteSize == 0)
break;
//保存数据
strData += (char *) byteBuffer;
}
bResult= TRUE;
}catch(...)
{
}
if(hInternetUrl != NULL)
InternetCloseHandle(hInternetUrl);
if(hInternetOpen != NULL)
InternetCloseHandle(hInternetOpen);
return bResult;
}
void MakeURL(CString & szURL)
{
//取系统本地时间
SYSTEMTIME st;
GetLocalTime(&st);
//
http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=GG&sty=GDRS&st=2&sr=1&p=1&ps=3000&js=&mkt=1&fd=%u-%u-31// sr 排序
// p 页面
// ps 每页记录数
// mkt 市场(上海,深圳)
// fd 季度
//4个季度 3 6 9 12
szURL.Format("
http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=GG&sty=GDRS&st=2&sr=1&p=1&ps=3000&js=&mkt=1&fd=%u-%u-31",st.wYear,(UINT)(st.wMonth / 3) * 3);
}
typedef struct _RJCGDATA
{
char szCode[7]; //股票代码
char szName[20]; //股票名称
DWORD dwGDCount; //股东数
DWORD dwRJCGCount; //人均持股数量
}RJCGDATA,*LPRJCGDATA;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
CString szURL;
MakeURL(szURL);
CString szConext;
if(!GetHttpContext(szURL,szConext))
return 0;
//UTF8 - ASCII
wchar_t * lpszWideChar = new wchar_t[ szConext .GetLength() ];
MultiByteToWideChar(CP_UTF8,0,szConext,-1,lpszWideChar,szConext.GetLength());
WideCharToMultiByte(CP_ACP,0,lpszWideChar,-1,szConext.GetBuffer(0),szConext.GetLength(),NULL,NULL);
szConext.ReleaseBuffer();
delete [] lpszWideChar;
//([" "])
szConext.Delete(0,3);
szConext.Delete(szConext.GetLength()-3 ,3);
szConext.Replace("\",\"","\n");
char szTempBuffer[100]={0};
LPCTSTR lpszOffset = szConext;
LPCTSTR lpszBegin = NULL , lpszEnd=NULL;
RJCGDATA RJCGData;
int i=0;
while(TRUE)
{
ZeroMemory(szTempBuffer,sizeof(szTempBuffer));
ZeroMemory(&RJCGData,sizeof(RJCGDATA));
lpszBegin = lpszOffset;
lpszEnd = strstr(lpszOffset,",");
strncpy(RJCGData.szCode,lpszBegin,lpszEnd - lpszBegin);
lpszBegin = lpszEnd + 1;
lpszEnd = strstr(lpszBegin,",");
strncpy(RJCGData.szName,lpszBegin,lpszEnd - lpszBegin);
lpszBegin = lpszEnd + 1;
lpszEnd = strstr(lpszBegin,",");
strncpy(szTempBuffer,lpszBegin,lpszEnd - lpszBegin);
RJCGData.dwGDCount= atoi(szTempBuffer);
lpszBegin = lpszEnd + 1;
lpszEnd = strstr(lpszBegin,",");
lpszBegin = lpszEnd + 1;
lpszEnd = strstr(lpszBegin,",");
strncpy(szTempBuffer,lpszBegin,lpszEnd - lpszBegin);
RJCGData.dwRJCGCount = atoi(szTempBuffer);
lpszOffset = strstr(lpszOffset,"\n");
if(lpszOffset == NULL)
break;
lpszOffset += strlen("\n");
printf("%04d %06s %8s %7u %6u\n",i++,RJCGData.szCode,RJCGData.szName,RJCGData.dwGDCount,RJCGData.dwRJCGCount);
}
return nRetCode;
}
百忙之中赶写而出。。没怎么测试。。好像也没啥测试的。。。
最后检测了下数据,确定真实有效,就行了。