visual c++网络通讯编程实用案例精选中
DWORD dwFlags;
InternetGetConnectedState(&dwFlags, 0);
CHAR strAgent[64];
sprintf(strAgent, "Agent%ld", timeGetTime());
HINTERNET hOpen;
if(!(dwFlags & INTERNET_CONNECTION_PROXY))
hOpen = InternetOpenA(strAgent, INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0);
else
hOpen = InternetOpenA(strAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(!hOpen)
{
AfxMessageBox("Internet连接错误!");
return -1;
}
DWORD dwSize;
CHAR szHead[] = "Accept: */*\r\n\r\n";
VOID* szTemp[16384];
HINTERNET hConnect;
CFile file;
if ( !(hConnect = InternetOpenUrlA ( hOpen, szUrl, szHead,
lstrlenA (szHead), INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0)))
{
AfxMessageBox("不能打开该URL!");
return -1;
}
if (file.Open(szFileName,CFile::modeWrite|CFile::modeCreate)==FALSE )
{
AfxMessageBox("不能打开本地的文件!");
return -1;
}
DWORD dwByteToRead = 0;
DWORD dwSizeOfRq = 4;
DWORD dwBytes = 0;
if (!HttpQueryInfo(hConnect, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER,
(LPVOID)&dwByteToRead, &dwSizeOfRq, NULL))
{
dwByteToRead = 0;
}
DWORD start;
DWORD end;
DWORD time;
CString tempstring;
time = 10;
start = timeGetTime();
do
{
if (!InternetReadFile (hConnect, szTemp, 16384, &dwSize))
{
AfxMessageBox("读文件出错!");
file.Close();
return -1;
}
if (dwSize==0)
break;
else
file.Write(szTemp,dwSize);
dwBytes+=dwSize;
if(dwByteToRead)
{
tempstring.Format("%d%%",(dwBytes*100)/dwByteToRead);
SetDlgItemText(IDC_PERCENT_TEXT,tempstring);
}
FLOAT fSpeed = 0;
fSpeed = (float)dwBytes;
fSpeed /= ((float)time)/1000.0f;
fSpeed /= 1024.0f;
tempstring.Format("%dKB/s",fSpeed);
SetDlgItemText(IDC_SPEED_TEXT,tempstring);
end = timeGetTime();
time = end - start;
if(time == 0)
time = 10;
}while (TRUE);
file.Close();
InternetCloseHandle(hOpen);
posted on 2010-03-27 11:01
漂漂 阅读(223)
评论(0) 编辑 收藏 引用 所属分类:
深入vc++