Windows API 之 InternetOpen、InternetOpenUrl、InternetReadFile

Windows API 之 InternetOpen、InternetOpenUrl、InternetReadFileInternetOpen:Initializesanapplication'suseoftheWinINetfunctions.HINTERNETInternetOpen(_

大家好,又见面了,我是你们的朋友全栈君。

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盘下生成了下载的文件:

<span role="heading" aria-level="2">Windows API 之 InternetOpen、InternetOpenUrl、InternetReadFile

 如果改为动态调用

#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;
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/155120.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • HTML与XML关系分析

    HTML与XML关系分析

    2021年12月14日
  • 数据库中的左连接和右连接的区别是什么_左连接右连接内连接图解

    数据库中的左连接和右连接的区别是什么_左连接右连接内连接图解数据库中的左连接和右连接的区别今天,别人问我一个问题:数据库中的左连接和右连接有什么区别?如果有A,B两张表,A表有3条数据,B表有4条数据,通过左连接和右连接,查询出的数据条数最少是多少条?最多是多少条?我被这个问题问住了,后来我去问了数据库开发人员,问到的答案也是各种各样:a最大12最小0b最大12最小未知c…

  • EditPlus正则表达式替换字符串详解

    EditPlus正则表达式替换字符串详解EditPlus的查找,替换,文件中查找支持以下的正则表达式:ExpressionDescription\tTabcharacter.\nNewline..Matchesanycharacter.|Eitherexpressiononitsleftandrightsidematchesthetargetstring.Forexa

  • 网络流媒体协议之——RTSP协议

    网络流媒体协议之——RTSP协议RTSP(Real-TimeStreamProtocol)协议是一个基于文本的多媒体播放控制协议,属于应用层。RTSP以客户端方式工作,对流媒体提供播放、暂停、后退、前进等操作。该标准由IETF指定,对应的协议是RFC2326。RTSP作为一个应用层协议,提供了一个可供扩展的框架,使得流媒体的受控和点播变得可能,它主要用来控制具有实时特性的数据的发送,但其本身并不用于传送流媒体数据,而

  • 简单说说USB协议(一)[通俗易懂]

    简单说说USB协议(一)[通俗易懂]USB,通用串行总线,是一种计算机与外围设备进行数据交互的通信协议。任何东西的出现都是有理可循的,在以前计算机刚兴起的时候,要连接一个新的设备,需要断电关闭计算机,连接好设备,配置好硬件,再上电运行,这个过程相当费时费力,为了能拥有随意接入设备这种热拔插特性,USB总线协议应运而生。USB总线如今已经有USB1.0、USB1.1、USB2.0、USB3.0。USB协议采用主从工作模式,…

  • 搭建Socks5代理服务器[通俗易懂]

    搭建Socks5代理服务器[通俗易懂]搭建Socks5代理服务器本篇介绍在centos7上搭建一个socks5的代理服务器。1、安装依赖软件包[root@sg~]#yuminstallgccgcc-c++pam-developenldap-developenssl-devel-y2、下载源码、解压、编译、安装[root@sg~]#wget–no-check-certificatehttps://jaist.dl.sourceforge.net/project/ss5/ss5/3.8.9-8/ss5-3.8

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号