WM下进行http下载、断点下载和上传(C++)

首先当然是利用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");
  
   SetEvent(hConnectedEvent);
  }
  break;
case 2: // Request handle
  switch(dwInternetStatus)
  {
  case INTERNET_STATUS_HANDLE_CREATED:
   {
    INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
    m_hRequest = (HINTERNET)pRes->dwResult;
    printf( "Request handle created/n");
    SetEvent(hRequestOpenedEvent);  
   }
   break;
  case INTERNET_STATUS_REQUEST_SENT:
   {
    DWORD *lpBytesSent = (DWORD*)lpStatusInfo;
    printf("Bytes Sent: %d/n", *lpBytesSent);
    //dwNumBytesComplete += *lpBytesSent;
   }
   break;
  case INTERNET_STATUS_REQUEST_COMPLETE:
   {
    INTERNET_ASYNC_RESULT *pAsyncRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
    printf("Function call finished" );
    printf("dwResult: %d/n" ,pAsyncRes->dwResult);
    printf( "dwError: %s/n ",pAsyncRes->dwError);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/psysx.html