zoukankan      html  css  js  c++  java
  • Windows API 之 InternetOpen、InternetOpenUrl、InternetReadFile

    InternetOpen:

    Initializes an application's use of the WinINet functions.

    HINTERNET InternetOpen(
      _In_ LPCTSTR lpszAgent,
      _In_ DWORD   dwAccessType,
      _In_ LPCTSTR lpszProxyName,
      _In_ LPCTSTR lpszProxyBypass,
      _In_ DWORD   dwFlags
    );

    Remarks

    InternetOpen is the first WinINet function called by an application. It tells the Internet DLL to initialize internal data structures and prepare for future calls from the application. When the application finishes using the Internet functions, it should call InternetCloseHandle to free the handle and any associated resources.

    InternetOpenUrl:

    Opens a resource specified by a complete FTP or HTTP URL.

    HINTERNET InternetOpenUrl(
      _In_ HINTERNET hInternet,
      _In_ LPCTSTR   lpszUrl,
      _In_ LPCTSTR   lpszHeaders,
      _In_ DWORD     dwHeadersLength,
      _In_ DWORD     dwFlags,
      _In_ DWORD_PTR dwContext
    );

    InternetReadFile:

    Reads data from a handle opened by the InternetOpenUrl, FtpOpenFile, or HttpOpenRequest function.

    BOOL InternetReadFile(
      _In_  HINTERNET hFile,
      _Out_ LPVOID    lpBuffer,
      _In_  DWORD     dwNumberOfBytesToRead,
      _Out_ LPDWORD   lpdwNumberOfBytesRead
    );
    #include <windows.h>
    #include <WinInet.h>
    #include <iostream>
    #pragma comment(lib,"wininet.lib")
    
    using namespace std;
    
    int DownLoader()
    {
        DWORD dwByteRead = 0;
        char szDownBuffer[4096];
        memset(szDownBuffer, 0, 4096);
        BOOL bIntNetReadFile = TRUE;
        BOOL bWriteFile = TRUE;
        DWORD dwWritten = 0;
    
        HINTERNET hintInternetOpen = InternetOpen("Testing", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
        if (!hintInternetOpen)
        {
            InternetCloseHandle(hintInternetOpen);
            return 1;
        }
        HINTERNET hintInternetOpenUrl = InternetOpenUrl(hintInternetOpen, "http://w.x.baidu.com/go/mini/2/10100", NULL, 0, INTERNET_FLAG_RELOAD, 0);
        if (!hintInternetOpenUrl)
        {
            InternetCloseHandle(hintInternetOpen);
            InternetCloseHandle(hintInternetOpenUrl);
            return 1;
        }
    
        HANDLE hCreateFile = CreateFile("D:\BaiDu2.exe", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
        if (hCreateFile == INVALID_HANDLE_VALUE)
        {
            InternetCloseHandle(hintInternetOpen);
            InternetCloseHandle(hintInternetOpenUrl);
            return 1;
        }
        
        while (bIntNetReadFile && bWriteFile)
        {
            bIntNetReadFile = InternetReadFile(hintInternetOpenUrl, szDownBuffer, sizeof(szDownBuffer), &dwByteRead);
            if (!dwByteRead)
            {
                break;
            }
    
            bWriteFile = WriteFile(hCreateFile, szDownBuffer, sizeof(szDownBuffer), &dwWritten, NULL);
            if (!dwWritten)
            {
                break;
            }
        }
        
        InternetCloseHandle(hintInternetOpen);
        InternetCloseHandle(hintInternetOpenUrl);
        return 0;
    }
    
    int main()
    {
        DownLoader();
        system("pause");
        return 0;
    }

    D盘下生成了下载的文件:

     如果改为动态调用

    #include <windows.h>
    #include <iostream>
    #include "dynamicfreetokill.h"
    
    using namespace std;
    
    int main()
    {
    
        cout << hmWinINetDLL << endl;
        if (!hmWinINetDLL)
        {
            cout << "HMDOULE" << endl;
        }
        pfINTERNETCRACKURL pfInternetCrackUrl = (pfINTERNETCRACKURL)GetProcAddress(hmWinINetDLL, "InternetCrackUrlW");
        cout << pfInternetCrackUrl << endl;
        if (!pfInternetCrackUrl)
        {
            cout << "InternetCrackUrlW" << endl;
        }
        pfINTERNETOPEN pfInternetOpen = (pfINTERNETOPEN)GetProcAddress(hmWinINetDLL, "InternetOpenW");
        cout << pfInternetOpen << endl;
        if (!pfInternetOpen)
        {
            cout << "InternetOpenW" << endl;
        }
        pfINTERNETCONNECT pfInternetConnect = (pfINTERNETCONNECT)GetProcAddress(hmWinINetDLL, "InternetConnectW");
        cout << pfInternetConnect << endl;
        if (!pfInternetOpen)
        {
            cout << "InternetConnectW" << endl;
        }
        pfHTTPOPENREQUEST pfHttpOpenRequest = (pfHTTPOPENREQUEST)GetProcAddress(hmWinINetDLL, "HttpOpenRequestW");
        cout << pfHttpOpenRequest << endl;
        if (!pfInternetOpen)
        {
            cout << "HttpOpenRequestW" << endl;
        }
        pfINTERNETSETOPTION pfInternetSetOption = (pfINTERNETSETOPTION)GetProcAddress(hmWinINetDLL, "InternetSetOptionW");
        cout << pfInternetSetOption << endl;
        if (!pfInternetOpen)
        {
            cout << "InternetSetOptionW" << endl;
        }
        pfHTTPSENDREQUEST pfHttpSendRequest = (pfHTTPSENDREQUEST)GetProcAddress(hmWinINetDLL, "HttpSendRequestW");
        cout << pfHttpSendRequest << endl;
        if (!pfInternetOpen)
        {
            cout << "HttpSendRequestW" << endl;
        }
        pfHTTPQUERYINFO pfHttpQueryInfo = (pfHTTPQUERYINFO)GetProcAddress(hmWinINetDLL, "HttpQueryInfoW");
        cout << pfHttpQueryInfo << endl;
        if (!pfInternetOpen)
        {
            cout << "HttpQueryInfoW" << endl;
        }
        pfINTERNETCLOSEHANDLE pfInternetCloseHandle = (pfINTERNETCLOSEHANDLE)GetProcAddress(hmWinINetDLL, "InternetCloseHandle");
        cout << pfInternetCloseHandle << endl;
        if (!pfInternetOpen)
        {
            cout << "InternetCloseHandle" << endl;
        }
        pfINTERNETSETFILEPOINTER pfInternetSetFilePointer = (pfINTERNETSETFILEPOINTER)GetProcAddress(hmWinINetDLL, "InternetSetFilePointer");
        cout << pfInternetSetFilePointer << endl;
        if (!pfInternetOpen)
        {
            cout << "InternetSetFilePointer" << endl;
        }
        pfINTERNETGETLASTRESPONSEINFO pfInternetGetLastResponseInfo = (pfINTERNETGETLASTRESPONSEINFO)GetProcAddress(hmWinINetDLL, "InternetGetLastResponseInfoW");
        cout << pfInternetGetLastResponseInfo << endl;
        if (!pfInternetOpen)
        {
            cout << "InternetGetLastResponseInfoW" << endl;
        }
        pfINTERNETREADFILE pfInternetReadFile = (pfINTERNETREADFILE)GetProcAddress(hmWinINetDLL, "InternetReadFile");
        cout << pfInternetReadFile << endl;
        if (!pfInternetOpen)
        {
            cout << "InternetReadFile" << endl;
        }
        
        FreeLibrary(hmWinINetDLL);
        system("pause");
        return 0;
    }
  • 相关阅读:
    IIS 备份
    Windows Service 的注册和卸载
    算法测试(课上测试)
    《Unix/Linux系统编程》第十四章学习笔记20191304商苏赫
    mystat
    实验四 Web服务器1socket编程
    《Unix/Linux系统编程》第十二章学习笔记20191304商苏赫
    整数范围与类型转换
    《Unix/Linux系统编程》第十三章学习笔记20191304商苏赫
    实验四 Web服务器2
  • 原文地址:https://www.cnblogs.com/predator-wang/p/4814236.html
Copyright © 2011-2022 走看看