Private Sub Command1_Click()
Dim http As WinHttp.WinHttpRequest
Dim imageUrls(1 To 5) As String
Dim imageFilePaths(1 To 5) As String
Dim byteArray() As Byte
Dim fileNumber As Integer
Dim i As Integer
' 设置要下载的 5 个图片 URL
imageUrls(1) = "https://s2.best-wallpaper.net/wallpaper/1920x1080/1603/Beautiful-universe-stars-galaxies-purple-color-nebula_1920x1080.jpg"
' 添加其他 4 个图片 URL
imageUrls(2) = "https://10wallpaper.com/wallpaper/1366x768/1207/white_magnolia_tree-Nature_Landscape_Wallpaper_1366x768.jpg"
imageUrls(3) = "https://10wallpaper.com/wallpaper/1366x768/1207/Nature_rivers_Landscape_Wallpaper_01_1366x768.jpg"
imageUrls(4) = "https://10wallpaper.com/wallpaper/1366x768/1207/Nature_rivers_Landscape_Wallpaper_02_1366x768.jpg"
imageUrls(5) = "https://10wallpaper.com/wallpaper/1366x768/1609/Mountains_greatness_silence_lake-Nature_High_Quality_Wallpaper_1366x768.jpg"
' 设置保存图片的本地路径模板,可根据需要修改
For i = 1 To 5
imageFilePaths(i) = i & ".jpg"
Next i
For i = 1 To 5
' 创建 WinHttpRequest 对象
Set http = New WinHttp.WinHttpRequest
' 发送 GET 请求
On Error GoTo ErrorHandler
http.Open "GET", imageUrls(i), False
http.Send
' 检查请求是否成功
If http.Status = 200 Then
' 获取响应体
byteArray = http.ResponseBody
' 打开文件并写入字节数据
fileNumber = FreeFile
Open imageFilePaths(i) For Binary Access Write As #fileNumber
Put #fileNumber, 1, byteArray
Close #fileNumber
MsgBox "图片 " & i & " 下载成功!"
Else
MsgBox "无法下载图片 " & i & ",状态码:" & http.Status
End If
Next i
Exit Sub
ErrorHandler:
MsgBox “出现错误:” & Err.Description
If fileNumber > 0 Then Close #fileNumber
End Sub