AutoIT中WinHttp.WinHttpRequest.5.1/MSXML2.XMLHTTP.6.0大文件下载失败求助
调用AutoIT的WinHttp.WinHttpRequest.5.1或MSXML2.XMLHTTP.6.0组件下载大型二进制文件时遇到异常:下载26MB文件时ResponseBody为空或不存在,但下载11MB文件完全正常。以下是完整代码及执行日志:
1. SSO认证代码
;管理员权限 #RequireAdmin ;错误处理 Global $__g_oHTTP_ErrorHandler = ObjEvent("AutoIt.Error", __HTTP_OnError) $local = @DesktopDir & "\test.bin" $lien = "https://mylink1-26Mb" ;$lien = "https://mylink2-11Mb" $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1") ;SSO认证请求 $url = "https://sso.authenticate" $target = "https://domain" $body = "USER=ABCD1234&PASSWORD=AZERTY&target=" & $target $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1") $oHTTP.Open("POST", $url, False) $oHTTP.Send($body) ;打印结果 ConsoleWrite($oHTTP.Status & @CR) ConsoleWrite($oHTTP.GetAllResponseHeaders & @CR)
执行日志:
200
Cache-Control: no-cache, private
Connection: Keep-Alive Date: Thu,25 Aug 2022 07:12:25 GMT
Keep-Alive: timeout=5, max=98
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: text/html;charset=UTF-8
Expires: -1
Server: Apache
Vary: Accept-Encoding
X-Vcap-Request-Id: ############################
2. HEAD请求获取文件大小
;发送HEAD请求获取文件大小 $oHTTP.Open("HEAD", $lien, False) $oHTTP.Send() ;打印结果 $size_cloud_file = $oHTTP.GetResponseHeader("Content-Length") ConsoleWrite($oHTTP.Status & @CR) ConsoleWrite($oHTTP.GetAllResponseHeaders & @CR)
执行日志:
200
Cache-Control: no-cache, private
Connection: Keep-Alive
Date: Thu, 25 Aug 2022 07:12:25 GMT
Keep-Alive: timeout=5, max=97
Pragma: no-cache
Content-Length: 26683497
Content-Type: text/plain;charset=UTF-8
Expires: 0
Server: Apache
Content-Description: File Transfer
Content-Disposition: attachment;filename=file.bin
X-Vcap-Request-Id: ########################
3. GET请求下载文件
;发送GET请求下载文件 $oHTTP.Open("GET", $lien, False) $oHTTP.Send() ;打印所有响应信息 Consolewrite("#Status : " & $oHTTP.Status & @CR) Consolewrite("#Status Text : " & $oHTTP.StatusText & @CR) Consolewrite("#GetAllResponseHeaders : " & @CR & $oHTTP.GetAllResponseHeaders & @CR) ;Consolewrite("Response Text : " & $oHTTP.ResponseText & @CR) if ($oHTTP.option(0)) Then Consolewrite("#UserAgentString : " & $oHTTP.option(0) & @CR) if ($oHTTP.option(1)) Then Consolewrite("#URL : " & $oHTTP.option(1) & @CR) if ($oHTTP.option(2)) Then Consolewrite("#URLCodePage : " & $oHTTP.option(2) & @CR) if ($oHTTP.option(3)) Then Consolewrite("#EscapePercentInURL : " & $oHTTP.option(3) & @CR) if ($oHTTP.option(4)) Then Consolewrite("#SslErrorIgnoreFlags : " & $oHTTP.option(4) & @CR) if ($oHTTP.option(5)) Then Consolewrite("#SelectCertificate : " & $oHTTP.option(5) & @CR) if ($oHTTP.option(6)) Then Consolewrite("#EnableRedirects : " & $oHTTP.option(6) & @CR) if ($oHTTP.option(7)) Then Consolewrite("#UrlEscapeDisable : " & $oHTTP.option(7) & @CR) if ($oHTTP.option(8)) Then Consolewrite("#UrlEscapeDisableQuery : " & $oHTTP.option(8) & @CR) if ($oHTTP.option(9)) Then Consolewrite("#SecureProtocols : " & $oHTTP.option(9) & @CR) if ($oHTTP.option(10)) Then Consolewrite("#EnableTracing : " & $oHTTP.option(10) & @CR) if ($oHTTP.option(11)) Then Consolewrite("#RevertImpersonationOverSsl : " & $oHTTP.option(11) & @CR) if ($oHTTP.option(12)) Then Consolewrite("#EnableHttpsToHttpRedirects : " & $oHTTP.option(12) & @CR) if ($oHTTP.option(13)) Then Consolewrite("#EnablePassportAuthentication : " & $oHTTP.option(13) & @CR) if ($oHTTP.option(14)) Then Consolewrite("#MaxAutomaticRedirects : " & $oHTTP.option(14) & @CR) if ($oHTTP.option(15)) Then Consolewrite("#MaxResponseHeaderSize : " & $oHTTP.option(15) & @CR) if ($oHTTP.option(16)) Then Consolewrite("#MaxResponseDrainSize : " & $oHTTP.option(16) & @CR) if ($oHTTP.option(17)) Then Consolewrite("#EnableHttp1_1 : " & $oHTTP.option(17) & @CR) if ($oHTTP.option(18)) Then Consolewrite("#EnableCertificateRevocationCheck : " & $oHTTP.option(18) & @CR)
执行日志:
#Status : 200
#Status Text : OK
#GetAllResponseHeaders :
Cache-Control: no-cache, private
Connection: Keep-Alive
Date: Thu, 25 Aug 2022 07:12:26 GMT
Keep-Alive: timeout=5, max=96
Pragma: no-cache
Content-Length: 26683497
Content-Type: text/plain;charset=UTF-8
Expires: 0
Server: Apache
Vary: Accept-Encoding
Content-Description: File Transfer
Content-Disposition: attachment;filename=file.bin
X-Vcap-Request-Id: #######################################UserAgentString : Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)
#URL : https://mylink1-26Mb
#URLCodePage : 65001
#EnableRedirects : True
#UrlEscapeDisableQuery : True
#RevertImpersonationOverSsl : True
#MaxAutomaticRedirects : 10
#MaxResponseHeaderSize : 65536
#MaxResponseDrainSize : 1024000
#EnableHttp1_1 : True
4. 写入文件及错误处理
;将ResponseBody写入本地二进制文件 FileDelete($local) $handle = FileOpen($local, 18) FileWrite($handle, $oHTTP.ResponseBody) ;=> 此处ResponseBody为空或不存在 FileClose($handle) Func __HTTP_OnError($oError) ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) EndFunc ;==>_MyCOMErrFunc
补充说明:使用MSXML2.XMLHTTP.6.0组件时出现完全相同的错误,求解决。
内容的提问来源于stack exchange,提问作者Jérémie Arruebo

