windows wininet api 实现下载web服务器文件(l历史代码,贴出共享)

#include <stdio.h>
#include<windows.h>
#include<wininet.h>
#include <string.h>
#pragma comment(lib,"wininet.lib")   
#include <process.h>
#define PROGRCESS_MIN_SIZE    0
#define PROGRCESS_MAX_SIZE    30
#define MAX_SIZE_BYTES       255
#define ONEK_SIZE_BYTES      1024
#define THREAD_DOWN_OK       100
#define THREAD_DOWN_ERR      101
#define THREAD_DOWN_QUIT     102
#define THREAD_FILE_ERROR    103
#define THREAD_CONN_ERROR    104


#define SAVED_FILE_PATH  "current.tmp"
#define TARGET_WEBURL "http://down.360safe.com/setup.exe"

DWORD dwRequestedFilelength=0;
DWORD dwWritedFileLength=0;
DWORD dwWritedBytes=0;
char  pzchBuffer[ONEK_SIZE_BYTES*10];
char  pzchResourceUrl[MAX_SIZE_BYTES];
DWORD dwReadBytes;
BOOL  bWriteResult;
BOOL  bInternetResult;
CRITICAL_SECTION gcs;

typedef struct _WINET_DATA_PARAM
{
    BOOL bThreadResult;
    HANDLE hOpenFile;
    HINTERNET hMyInternet;
    HINTERNET hOpenUrlInternet;
}WINET_DATA_PARAM,*LPWINET_DATA_PARAM;

typedef struct _PROCESS_DATA_PARAM
{
    BOOL  bLoop;
    DWORD dwLoop;
    DWORD dwStarsCount;
    DWORD dwLinesCount;
}PROCESS_DATA_PARAM,*LPPROCESS_DATA_PARAM;


unsigned int WINAPI ThreadProcDownload(LPVOID lparam)
{
    //initiate wininet library
    LPWINET_DATA_PARAM lpwdp=(LPWINET_DATA_PARAM)lparam;

    lpwdp->hMyInternet=InternetOpen("NSA100",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
    if (lpwdp->hMyInternet==NULL)
    {
        lpwdp->bThreadResult=THREAD_DOWN_QUIT;
        return lpwdp->bThreadResult;
    }
    //Estiblish  URL  Session 
    lpwdp->hOpenUrlInternet=InternetOpenUrl(lpwdp->hMyInternet,TARGET_WEBURL,NULL,0,INTERNET_FLAG_RELOAD,0);
    if (lpwdp->hOpenUrlInternet==NULL)
    {
        lpwdp->bThreadResult=THREAD_CONN_ERROR;
        return lpwdp->bThreadResult;
    }
    //create file and stored data
    lpwdp->hOpenFile=CreateFile(SAVED_FILE_PATH,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    if (lpwdp->hOpenFile==INVALID_HANDLE_VALUE)
    {
        lpwdp->bThreadResult=THREAD_FILE_ERROR;
        return THREAD_FILE_ERROR;
    }
    while(TRUE)
    {
        bInternetResult=InternetReadFile(lpwdp->hOpenUrlInternet,pzchBuffer,sizeof(pzchBuffer),&dwReadBytes);
        if (dwReadBytes==0)
            break;

        bWriteResult=WriteFile(lpwdp->hOpenFile,pzchBuffer,dwReadBytes/*strlen(buffer)*/,&dwWritedBytes,NULL);
        if (bWriteResult!=0)
        {   
            dwRequestedFilelength+=dwReadBytes;
            dwWritedFileLength+=dwWritedBytes;
        }
        dwReadBytes=0;
        dwWritedBytes=0;
    }
    if(dwWritedFileLength!=dwRequestedFilelength)
    {
        DeleteFile(SAVED_FILE_PATH);
        lpwdp->bThreadResult=THREAD_DOWN_ERR;
        return lpwdp->bThreadResult;
    }
    InternetCloseHandle(lpwdp->hMyInternet);
    InternetCloseHandle(lpwdp->hOpenUrlInternet);
    CloseHandle(lpwdp->hOpenFile);
    lpwdp->bThreadResult=THREAD_DOWN_OK;
    return lpwdp->bThreadResult;
}

unsigned int WINAPI ThreadProcProgress(LPVOID lpvoid)
{
    LPPROCESS_DATA_PARAM lpdp=(LPPROCESS_DATA_PARAM)lpvoid;
    while ((lpdp->bLoop)==TRUE)
    {
        EnterCriticalSection(&gcs);
        for (int dwNum=0;dwNum<lpdp->dwLoop;dwNum++)
        {
            printf("*");
        }
        lpdp->dwLinesCount++;
        lpdp->dwStarsCount=lpdp->dwLinesCount*lpdp->dwLoop;
        printf("-->>>lines: %d starscount: %d\n",lpdp->dwLinesCount,lpdp->dwStarsCount);
        LeaveCriticalSection(&gcs);
        Sleep(10);
    }
    return 1;
}
int main(void)
{
    memset(pzchBuffer,0,sizeof(pzchBuffer));
    memset(pzchResourceUrl,0,sizeof(pzchResourceUrl));
    strcpy(pzchResourceUrl,TARGET_WEBURL);
    InitializeCriticalSection(&gcs);
    WINET_DATA_PARAM wdp;
    PROCESS_DATA_PARAM pdp;
    pdp.bLoop=TRUE;
    pdp.dwLoop=PROGRCESS_MAX_SIZE;
    pdp.dwLinesCount=0;
    pdp.dwStarsCount=0;
    HANDLE hDownloadThread = (HANDLE)_beginthreadex(nullptr, 0,ThreadProcDownload, (LPVOID)&wdp, 0, nullptr);
    HANDLE hProgressThread = (HANDLE)_beginthreadex(nullptr, 0,ThreadProcProgress, (LPVOID)&pdp, 0, nullptr);
    SYSTEMTIME sStartTime;
    GetSystemTime(&sStartTime);
    WaitForSingleObject(hDownloadThread,INFINITE);
    pdp.bLoop=FALSE;
    pdp.dwLoop=PROGRCESS_MIN_SIZE;
    WaitForSingleObject(hProgressThread,INFINITE);
    switch (wdp.bThreadResult)
    {
    case THREAD_DOWN_QUIT:
        printf("initiate wininet functions fail\n");
        break;
    case THREAD_FILE_ERROR:
        printf("creating starget file  fail\n");
        break;
    case THREAD_DOWN_ERR:
        printf("some data is  missing\n");
        break;
    case THREAD_CONN_ERROR:
        printf("the connection  fail\n");
        break;
    case THREAD_DOWN_OK:
        printf("the file downloaded successfully \n");
        break;
    default:
        printf("there is default param \n");
        break;
    }
    CloseHandle(hDownloadThread);
    CloseHandle(hProgressThread);
    SYSTEMTIME sEndTime;
    GetSystemTime(&sEndTime);
    printf("START TIME :%d:%d:%d\n",sStartTime.wHour,sStartTime.wMinute,sStartTime.wSecond);
    printf("END   TIME :%d:%d:%d\n",sEndTime.wHour,sEndTime.wMinute,sEndTime.wSecond);
    system("pause");
    return 1;
}

已测试,360离线文件下载。

说明一下,喜欢的自己可以改成命令行下载工具。

你可能感兴趣的:(windows wininet api 实现下载web服务器文件(l历史代码,贴出共享))