设为首页 加入收藏

TOP

WM下进行http下载、断点下载和上传(C++)(一)
2014-11-24 12:07:20 来源: 作者: 【 】 浏览:1
Tags:进行 http 下载 断点 下载和 上传

首先当然是利用Wininet.lib库了,添加头文件#include "WinInet.h"和库#pragma comment(lib,"Wininet.lib"),本人最开始时使用同步下载,但是发现下载600K左右的文件都不能下下来,网络自己会断掉,很奇怪,于是我采用异步方式。


#include "stdafx.h"
#include "WinInet.h"
#pragma comment(lib,"Wininet.lib")
void CALLBACK InternetProc(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength);


void test()
{


char *buffer = new char[2024] ;
hConnectedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
hRequestOpenedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
hRequestCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);


m_hSession = InternetOpen(L"ttt", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
// asynchronous transfer mode
if (m_hSession == NULL)
{
printf("Internet open error!");
return;


}
if(InternetSetStatusCallback(m_hSession, (INTERNET_STATUS_CALLBACK)InternetProc)
== INTERNET_INVALID_STATUS_CALLBACK)
{
printf("创建回调失败!");
}




m_hConnection = InternetConnect(m_hSession, L"dt.163.com", 80,L"",L"",INTERNET_SERVICE_HTTP, 0, 1);


if (m_hConnection == NULL)
{
if (GetLastError() != ERROR_IO_PENDING)
{
printf( "InternetConnect failed, error " );
return;
}
WaitForSingleObject(hConnectedEvent, INFINITE);
}


m_hRequest = HttpOpenRequest(m_hConnection,L"GET",L"/images/news/0605/news02053101_5.jpg",
HTTP_VERSION, NULL, 0, INTERNET_FLAG_DONT_CACHE, 2); //HTTP_VERSION "HTTP/1.0"


if (m_hRequest == NULL)
{


if (GetLastError() != ERROR_IO_PENDING)
{
printf("InternetRequest Failure!");
return;
}
// Wait until we get the request handle
WaitForSingleObject(hRequestOpenedEvent, INFINITE);


}
BOOL bSendRequest = HttpSendRequest(m_hRequest, NULL, 0, 0, 0);
if (bSendRequest == FALSE)
{
printf("SendRequest Failure!");

}


WaitForSingleObject(hRequestCompleteEvent, INFINITE);



BOOL hwrite;
DWORD written;
HANDLE createfile;
createfile=CreateFile(L"//sss.jpg",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if (createfile==INVALID_HANDLE_VALUE)
{
printf("Create jpg error!");
return;
}
TCHAR bufQuery[32] = {0};
DWORD dwLengthBufQuery = sizeof(bufQuery);
HttpQueryInfo(m_hRequest, HTTP_QUERY_CONTENT_LENGTH, bufQuery, &dwLengthBufQuery, NULL);


DWORD dwFileSize = (DWORD)_wtol(bufQuery); //get the length of received file
printf("%d/n",dwFileSize);


DWORD dwBytesRead;


while(1)
{
InternetReadFile(m_hRequest,buffer,sizeof(buffer),&dwBytesRead);
if(dwBytesRead == 0)
break;
hwrite=WriteFile(createfile,buffer,sizeof(buffer),&written,NULL);
if (hwrite==0)
{
printf("Write error!");
break ;
}
}



delete buffer;
CloseHandle(createfile);
InternetCloseHandle(m_hSession);
InternetCloseHandle(m_hConnection);
InternetCloseHandle(m_hRequest);


}
void CALLBACK InternetProc(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
LPVOID lpStatusInfo, DWORD dwStatusInformationLength)
{
printf( "Callback dwInternetStatus: %d/r" , dwInternetStatus);



switch(dwContext)
{
case 1: // Connection handle
if (dwInternetStatus == INTERNET_STATUS_HANDLE_CREATED)
{
INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
m_hConnection = (HINTERNET)pRes->dwResult;
printf( "Connect handle created/n");

SetE

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇原生Android 4.0来电按钮消失的问.. 下一篇如何利用JLINK烧写U-boot到NAND F..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)