3,055
社区成员
发帖
与我相关
我的任务
分享
CString strURL = "http://query.sse.com.cn/commonQuery.do?jsonCallBack=&isPagination=false&sqlId=COMMON_SSE_XXPL_JYXXPL_DZJYXX_LATEST_L&stockId=&startDate=2015-12-22&endDate=2015-12-24&pageHelp.pageSize=20&pageHelp.cacheSize=5&_=";
CString strRefer = "http://www.sse.com.cn/disclosure/diclosure/block";
CString strFile = "D:\\test.txt";
CInternetSession Session( "Internet Explorer"
, 0
, INTERNET_OPEN_TYPE_PRECONFIG
, NULL
, NULL
, INTERNET_FLAG_DONT_CACHE );
CString szUrlAccessError;
CHttpConnection *pServer = NULL;
CHttpFile *pHttpFile = NULL;
TRY
{
DWORD dwServiceType;
CString strServer;
CString strObject;
INTERNET_PORT nPort;
BOOL bParsed = AfxParseURL(strURL, dwServiceType, strServer, strObject, nPort);
if (bParsed)
{
pServer = Session.GetHttpConnection(strServer, nPort);
if (pServer)
{
DWORD dwRet = 0;
pHttpFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, strRefer, 1, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION);
if (pHttpFile)
{
CFile file;
BOOL bSendRequest = pHttpFile->SendRequest();
if (bSendRequest)
{
pHttpFile->QueryInfoStatusCode(dwRet);
if (HTTP_STATUS_OK == dwRet)
{
CString str;
pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, str);
DWORD dwFileLen = _ttol(str);
DWORD dwFileLenReceived = 0;
BYTE buf[1024];
if (file.Open(strFile, CFile::modeCreate|CFile::modeWrite))
{
TRY
{
UINT uSize = 0;
uSize = pHttpFile->Read(buf, 1024);
do
{
file.Write(buf, uSize);
dwFileLenReceived += (DWORD)uSize;
}while (uSize = pHttpFile->Read(buf, 1024));
}
CATCH_ALL(e)
{
CString szError;
e->GetErrorMessage( szError.GetBuffer(1024), 1024 );
szError.ReleaseBuffer();
szUrlAccessError.Format( "访问URL(%s)发生异常: %s", strURL, szError );
}
END_CATCH_ALL;
file.Close();
}
if (dwFileLenReceived == 0)
{
szUrlAccessError.Format("下载文件有错:实际下载大小%u", dwFileLenReceived);
}
}
else
{
szUrlAccessError.Format( "HTTP返回值错误: %u, %s", dwRet, strURL );
}
}
pHttpFile->Close();
delete pHttpFile;
}
else
{
szUrlAccessError.Format( "打开Internet对象出错: %s", strURL );
}
delete pServer;
}
else
{
szUrlAccessError.Format( "建立Internet连接出错: %s", strURL );
}
}
else
{
szUrlAccessError.Format( "URL解析出错:%s", strURL );
}
}
CATCH_ALL(e)
{
if (pHttpFile)
{
pHttpFile->Close();
delete pHttpFile;
}
if (pServer)
{
pServer->Close();
delete pServer;
}
CString szError;
e->GetErrorMessage( szError.GetBuffer(1024), 1024 );
szError.ReleaseBuffer();
szUrlAccessError.Format( "访问URL(%s)发生异常: %s", strURL, szError );
}
END_CATCH_ALL
Session.Close();