AutoHotkey 语言 利用 WinHTTP 进行文件下载

AutoHotkey阿木 发布于 2025-06-12 14 次阅读


AutoHotkey Language: File Downloading with WinHTTP

AutoHotkey is a powerful scripting language for automating Windows tasks. It allows users to create scripts that can automate repetitive tasks, simulate keyboard and mouse inputs, and interact with various Windows applications. One of the interesting features of AutoHotkey is its ability to use the WinHTTP API for downloading files from the internet. In this article, we will explore how to use WinHTTP in AutoHotkey to download files from a specified URL.

Introduction to WinHTTP

WinHTTP is a Windows API that provides a simple interface for sending HTTP requests and receiving responses. It is commonly used for downloading files, uploading files, and performing other HTTP-related tasks. WinHTTP is available on all versions of Windows, making it a reliable choice for automating internet tasks.

Setting Up Your AutoHotkey Environment

Before you start writing your script, make sure you have AutoHotkey installed on your system. You can download the latest version of AutoHotkey from the official website (https://www.autohotkey.com/). Once installed, you can create a new script file with a `.ahk` extension.

Basic WinHTTP Download Script

The following is a basic example of an AutoHotkey script that uses WinHTTP to download a file from a specified URL:

ahk
; Define the URL and local file path
url := "http://example.com/file.zip"
localFilePath := "C:Downloadfile.zip"

; Initialize WinHTTP session
WinHttp := ComObjCreate("WinHttp.WinHttpRequest.5.1")

; Set the URL for the download
WinHttp.Open("GET", url)

; Send the request
WinHttp.Send()

; Check if the request was successful
if (WinHttp.Status == 200) {
; Write the response to a file
FileAppend, %WinHttp.ResponseBody, %localFilePath%
MsgBox, File downloaded successfully to %localFilePath%
} else {
MsgBox, Error downloading file: %WinHttp.Status%
}

In this script, we first define the URL of the file we want to download and the local file path where we want to save it. We then create a new WinHTTP session using `ComObjCreate` and open a GET request to the specified URL. After sending the request, we check the response status. If the status is 200 (OK), we write the response body to the local file path using `FileAppend`. If the status is not 200, we display an error message.

Advanced Features

The basic script provided above is a good starting point for downloading files using WinHTTP in AutoHotkey. However, there are several advanced features and considerations to keep in mind:

Handling Redirects

Sometimes, the server might redirect the request to a different URL. To handle redirects, you can check the `WinHttp.ResponseHeaders` object for the "Location" header and update the URL accordingly.

ahk
if (WinHttp.ResponseHeaders.Location) {
MsgBox, Redirecting to %WinHttp.ResponseHeaders.Location%
WinHttp.Open("GET", WinHttp.ResponseHeaders.Location)
WinHttp.Send()
}

Handling Authentication

If the file requires authentication, you can include the necessary credentials in the request headers. For example, to use Basic Authentication, you can use the following code:

ahk
auth := "Basic " . Base64Encode("username:password")
WinHttp.SetRequestHeader("Authorization", auth)

Monitoring Progress

WinHTTP does not provide a built-in way to monitor the download progress. However, you can use the `WinHttp.Read` method to read chunks of the response body and keep track of the progress manually.

ahk
totalBytes := WinHttp.GetResponseHeader("Content-Length")
downloadedBytes := 0
chunkSize := 1024

Loop {
bytesRead := WinHttp.Read(chunk, chunkSize)
if (bytesRead == 0) {
break
}
downloadedBytes += bytesRead
percentComplete := (downloadedBytes / totalBytes) 100
MsgBox, Downloading... %percentComplete% complete
}

Error Handling

It's important to handle errors that may occur during the download process. You can check the `WinHttp.Status` and `WinHttp.StatusText` properties to get more information about the error.

ahk
if (WinHttp.Status != 200) {
MsgBox, Error downloading file: %WinHttp.StatusText%
}

Conclusion

Using WinHTTP in AutoHotkey to download files from the internet is a straightforward process. By following the steps outlined in this article, you can create scripts that automate the download of files from specified URLs. With the advanced features and considerations mentioned, you can enhance your scripts to handle redirects, authentication, progress monitoring, and error handling. Happy scripting!