您的位置:首页 > 理论基础 > 计算机网络

支持的代理模式异步下载httpdownload

2012-10-31 11:22 811 查看
typedef struct HTTP

{

DWORD CompleteResult;

HINTERNET Handle;

}http;

static void CALLBACK InternetCallback(HINTERNET hInternet,

DWORD dwContext,

DWORD dwInternetStatus,

LPVOID lpvStatusInformation,

DWORD dwStatusInformationLength)

{

DebugPrint(1, _T("InternetCallback = 0X%X"), dwInternetStatus);

if (dwInternetStatus == INTERNET_STATUS_REQUEST_COMPLETE)

{

INTERNET_ASYNC_RESULT *Result = (INTERNET_ASYNC_RESULT*)lpvStatusInformation;

http *p = (http*)dwContext;

p->CompleteResult = Result->dwResult;

g_event.SetEvent();

}

}

//异步模式下载

int httpDownloadEx(CAutoUpdateDlg *pDlg, stdString strURL, stdString strFilePath)

{

DWORD byteread=0;

char buffer[20*1024] = {0};

memset(buffer,0,20*1024);

HINTERNET internetopen;

DWORD dwStatusSize;

DWORD m_dwStatusCode = MAX_PATH;

DWORD dwRet = 0;

BOOL hwrite;

DWORD written;

HANDLE createfile;

HINTERNET internetopenurl;

BOOL internetreadfile;

DWORD dwtoal=0;

http *p;

p = new http;

DebugPrint(1, _T("Start httpDownloadEx %s"), strURL.c_str());

internetopen=InternetOpen(_T("SP_InetStatus/1.0"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL, INTERNET_FLAG_ASYNC);

if (internetopen==NULL)

{

return 1;

}

InternetSetStatusCallback(internetopen, InternetCallback);

ReSend:

internetopenurl = InternetOpenUrl(internetopen, strURL.c_str(), NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_NO_CACHE_WRITE, (DWORD)p);

if (internetopenurl || GetLastError() != ERROR_IO_PENDING)

{

;

}

else

{

if ( WaitForSingleObject(g_event,300000) == WAIT_OBJECT_0 )

{

internetopenurl = (HINSTANCE)p->CompleteResult;

}

else

{

internetopenurl = NULL;

dwRet = 1;

goto there;

}

}

dwStatusSize = sizeof(m_dwStatusCode);

if (FALSE == ::HttpQueryInfo(internetopenurl,

HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,

&m_dwStatusCode,

&dwStatusSize,

NULL)) //获取返回状态码

{

dwRet = 1;

goto here;

}

if(HTTP_STATUS_PROXY_AUTH_REQ == m_dwStatusCode)

{

DWORD dwError = 0;

if((dwError = ::InternetErrorDlg(pDlg->GetSafeHwnd(), internetopenurl, ERROR_INTERNET_INCORRECT_PASSWORD, FLAGS_ERROR_UI_FILTER_FOR_ERRORS|FLAGS_ERROR_UI_FLAGS_GENERATE_DATA|FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL)) == ERROR_INTERNET_FORCE_RETRY)

{

goto ReSend;

}

else

{

InternetCloseHandle(internetopenurl);

InternetCloseHandle(internetopen);

return 1;

}

}

createfile=CreateFile(strFilePath.c_str(),GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);

if (createfile==INVALID_HANDLE_VALUE)

{

dwRet = 1;

goto next;

}

while(1)

{

INTERNET_BUFFERS InetBuff;

FillMemory(&InetBuff, sizeof(InetBuff), 0);

InetBuff.dwStructSize = sizeof(InetBuff);

InetBuff.lpvBuffer = buffer;

InetBuff.dwBufferLength = sizeof(buffer);

DWORD dwtime = GetTickCount();

internetreadfile=InternetReadFileEx(internetopenurl,&InetBuff,IRF_ASYNC, (DWORD)p);

if(internetreadfile == NULL)

{

if(GetLastError() == ERROR_IO_PENDING)

{

if ( WaitForSingleObject(g_event,300000) == WAIT_OBJECT_0 )

{

;

}

else

{

dwRet = 1;

goto here;

}

}

else

{

dwRet = 1;

goto here;

}

}

if (InetBuff.dwBufferLength == 0)

{

break;

}

buffer[InetBuff.dwBufferLength] = 0;

hwrite=WriteFile(createfile,InetBuff.lpvBuffer,InetBuff.dwBufferLength,&written,NULL);

if (hwrite==0)

{

dwRet = 1;

goto here;

}

else

{

DWORD time= GetTickCount() - dwtime;

dwtoal += time;

DebugPrint(1, _T("WriteFile Datalen = 0X%X, time = %d"), InetBuff.dwBufferLength, time);

}

}

here:

CloseHandle(createfile);

next:

InternetCloseHandle(internetopenurl);

there:

InternetCloseHandle(internetopen);

InternetSetStatusCallback(internetopen, NULL);

DebugPrint(1, _T("End httpDownloadEx %s, all time = %d"), strURL.c_str(), dwtoal);

return dwRet;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: