支持的代理模式异步下载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;
}
{
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;
}
相关文章推荐
- NO.19 HttpConnection、HttpClient使用注意点(附http连接工具:采用HttpClient支持代理Proxy、支持用户名密码、支持域Domain模式)
- 简单封装下rest api,支持http,https及代理模式
- NSURLConnection--异步下载HTTP Get
- Android异步框架android-async-http-master的下载导入和错误处理
- QtDownloadTool——简单的HTTP资源下载工具
- 简单实现Http代理工具--完善支持QQ代理
- ASIHTTPRequest 异步下载
- IOS开发网络篇之──ASIHTTPRequest下载示例(支持断点续传)
- 流媒体技术{包括Http渐进式下载,即html5 video audio标签支持的协议}
- HTTP代理实现请求报文的拦截与篡改2--功能介绍+源码下载
- [IOS_HTTP]ASIHTTPRequest下载示例(支持断点续传)
- C#实现http协议支持上传下载文件的GET、POST请求
- Qt 之 HTTP 请求下载(支持断点续传)
- IOS开发网络篇之──ASIHTTPRequest下载示例(支持断点续传)
- F#中的异步及并行模式(3 - 上):代理的基本使用
- F#中的异步和并行设计模式(三):代理
- unity3d之http多线程异步资源下载
- 天哪,我遇上了一个windows api BUG! 并贡献一个完全支持代理服务器的http文件下载VC代码
- 重构桌面飘着圣诞老人,利用策略模式和改造的代理模式让软件完全实现开闭原则,欢迎下载源代码分析
- Unity3d C# HttpWebRequest 异步下载文件