18,356
社区成员
发帖
与我相关
我的任务
分享
/********************************************************************
created: 2009/06/11
created: 11:6:2009 19:31
filename: inet2\main.cpp
file path: inet2
file base: main
file ext: cpp
author: FallingStar
purpose: MyInet
*********************************************************************/
#include<afx.h>
#include<wininet.h>
#include<process.h>
#include<iostream>
#include<fstream>
using namespace std;
#pragma comment(lib,"wininet.lib")
#define _CRTDBG_MAP_ALLOC
#include<stdlib.h>
#define UNICODE
#define _UNICODE
#include<crtdbg.h>
#define TIMEOUT 3000
void __stdcall Callback(HINTERNET hInternet,
DWORD dwContext,
DWORD dwInternetStatus,
LPVOID lpStatusInfo,
DWORD dwStatusInfoLen);
CString ToString (DWORD i)
{
CString str;
str.Format("%d",i);
return str;
}
HINTERNET hSession;
HINTERNET hFile;
HANDLE hComplete;
HANDLE CloseFile;
int i=250020;
void GetTitle()
{
}
void main()
{
hComplete=CreateEvent(NULL,FALSE,FALSE,NULL);
CloseFile=CreateEvent(NULL,FALSE,FALSE,NULL);
hSession = InternetOpen(NULL,
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
INTERNET_FLAG_ASYNC);
if (InternetSetStatusCallback(hSession,
(INTERNET_STATUS_CALLBACK)&Callback) == INTERNET_INVALID_STATUS_CALLBACK)
{
cout<<"回调函数注册失败"<<endl;
return ;
}
for(UINT i=1;i<2;i++)
{
ResetEvent(CloseFile);
ResetEvent(hComplete);
CString url="http://vic.sina.com.cn/res/sh/beingmate/images/tt.jpg";
// CString url="http://www.sina.com.cn"; 这里换成新浪,400多K也能下完
hFile =InternetOpenUrl(hSession,url,NULL,0,INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_PRAGMA_NOCACHE| INTERNET_FLAG_RELOAD,1);
if(WaitForSingleObject(hComplete,3000)==WAIT_TIMEOUT)
{
fstream f;
f.open("1.txt",ios::out|ios::app);
cout<<"超时"<<endl;
CString t;
CTime time=CTime::GetCurrentTime();
t=time.Format("%H:%M:%S");
f<<(LPCSTR)t<<" "<<i<<" "<<"超时"<<endl;
}
else{
bool bAllDone=false;
fstream f;
f.open(ToString(i)+".htm",ios::out|ios::app);
do
{
char lpReadBuff[512];
INTERNET_BUFFERS InetBuff;
bool outed=false;
ResetEvent(hComplete);
memset(lpReadBuff,0,512);
FillMemory(&InetBuff, sizeof(InetBuff), 0);
InetBuff.dwStructSize = sizeof(InetBuff);
InetBuff.lpvBuffer = lpReadBuff;
InetBuff.dwBufferLength = sizeof(lpReadBuff) - 1;
BOOL bOk=InternetReadFileEx(hFile,
&InetBuff,
IRF_ASYNC, 1);
if(!bOk&&GetLastError()==ERROR_IO_PENDING)
{
WaitForSingleObject(hComplete,INFINITE);
}
f<<lpReadBuff;
if (InetBuff.dwBufferLength == 0)
{
bAllDone = TRUE; //这里判断应该没有问题的
}
}while(bAllDone==false);
f.close();
}
cout<<i<<endl;
InternetCloseHandle(hFile);
hFile=NULL;
WaitForSingleObject(CloseFile,INFINITE);
}
InternetSetStatusCallback(hSession,NULL);
InternetCloseHandle(hSession);
_CrtDumpMemoryLeaks();
return ;
}
void __stdcall Callback(HINTERNET hInternet,
DWORD dwContext,
DWORD dwInternetStatus,
LPVOID lpStatusInfo,
DWORD dwStatusInfoLen)
{
cout<<dwInternetStatus<<endl;
switch(dwInternetStatus)
{
case INTERNET_STATUS_HANDLE_CREATED:
{
INTERNET_ASYNC_RESULT* res = (INTERNET_ASYNC_RESULT*)lpStatusInfo;
hFile = (HINTERNET)(res->dwResult);
}
break;
case INTERNET_STATUS_REQUEST_COMPLETE:
{
SetEvent(hComplete);
}
break;
case INTERNET_STATUS_HANDLE_CLOSING:
SetEvent(CloseFile);
break;
case INTERNET_STATUS_CONNECTION_CLOSED:
break;
}
return;
}