当前位置:   article > 正文

[AHK] WinHttpRequest.5.1报错 0x80092004 找不到对象或属性

[AHK] WinHttpRequest.5.1报错 0x80092004 找不到对象或属性

目录

背景描述

 用浏览器访问,正常返回

​编辑

 AHK v2官方示例源代码

 AHK v2运行结果报错(0x80092004) 找不到对象或属性

用thqby大佬的WinHttpRequest.ahk库测试报错 0x80092004 找不到对象或属性

附:

用Apifox访问,也正常返回

AHK v1 官方示例源代码

AHK v1运行失败报错 0x80092004 找不到对象或属性

用Msxml2.XMLHTTP则能正常得到结果



背景描述

想通过ahk编程,获取 https://autohotkey.com/download/2.0/version.txt  的文件内容。

在确认用浏览器可以正常访问情况下,用官方帮助中的示例运行会报错 Error: (0x80092004) 找不到对象或属性。用 thqby大佬的 WinHttpRequest.ahk 网络请求库 https://github.com/thqby/ahk2_lib/blob/master/WinHttpRequest.ahk  也报同样错误。

求指点

 用浏览器访问,正常返回

 AHK v2官方示例源代码

  1. whr := ComObject("WinHttp.WinHttpRequest.5.1")
  2. whr.Open("GET", "https://autohotkey.com/download/2.0/version.txt", true)
  3. whr.Send()
  4. whr.WaitForResponse()
  5. version := whr.ResponseText
  6. MsgBox version

 AHK v2运行结果报错(0x80092004) 找不到对象或属性

如果把参数 true改为 false ,报错如下:

用thqby大佬的WinHttpRequest.ahk库测试报错 0x80092004 找不到对象或属性

  1. /************************************************************************
  2. * @file: WinHttpRequest.ahk
  3. * @description: 网络请求库
  4. * @author thqby
  5. * @date 2021/08/01
  6. * @version 0.0.18
  7. ***********************************************************************/
  8. #Requires AutoHotkey v2.0
  9. whr := WinHttpRequest()
  10. whr.Open("Get", "https://www.autohotkey.com/download/2.0/version.txt", True)
  11. whr.Send()
  12. whr.WaitForResponse()
  13. MsgBox whr.ResponseText
  14. class WinHttpRequest {
  15. static AutoLogonPolicy := {
  16. Always: 0,
  17. OnlyIfBypassProxy: 1,
  18. Never: 2
  19. }
  20. static Option := {
  21. UserAgentString: 0,
  22. URL: 1,
  23. URLCodePage: 2,
  24. EscapePercentInURL: 3,
  25. SslErrorIgnoreFlags: 4,
  26. SelectCertificate: 5,
  27. EnableRedirects: 6,
  28. UrlEscapeDisable: 7,
  29. UrlEscapeDisableQuery: 8,
  30. SecureProtocols: 9,
  31. EnableTracing: 10,
  32. RevertImpersonationOverSsl: 11,
  33. EnableHttpsToHttpRedirects: 12,
  34. EnablePassportAuthentication: 13,
  35. MaxAutomaticRedirects: 14,
  36. MaxResponseHeaderSize: 15,
  37. MaxResponseDrainSize: 16,
  38. EnableHttp1_1: 17,
  39. EnableCertificateRevocationCheck: 18,
  40. RejectUserpwd: 19
  41. }
  42. static PROXYSETTING := {
  43. PRECONFIG: 0,
  44. DIRECT: 1,
  45. PROXY: 2
  46. }
  47. static SETCREDENTIALSFLAG := {
  48. SERVER: 0,
  49. PROXY: 1
  50. }
  51. static SecureProtocol := {
  52. SSL2: 0x08,
  53. SSL3: 0x20,
  54. TLS1: 0x80,
  55. TLS1_1: 0x200,
  56. TLS1_2: 0x800,
  57. All: 0xA8
  58. }
  59. static SslErrorFlag := {
  60. UnknownCA: 0x0100,
  61. CertWrongUsage: 0x0200,
  62. CertCNInvalid: 0x1000,
  63. CertDateInvalid: 0x2000,
  64. Ignore_All: 0x3300
  65. }
  66. __New(UserAgent := unset) {
  67. (this.whr := ComObject('WinHttp.WinHttpRequest.5.1')).Option[0] := IsSet(UserAgent) ? UserAgent : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36 Edg/89.0.774.68'
  68. }
  69. request(url, method := 'GET', post_data?, headers := {}) {
  70. this.Open(method, url)
  71. for k, v in headers.OwnProps()
  72. this.SetRequestHeader(k, v)
  73. this.Send(post_data?)
  74. return this.ResponseText
  75. }
  76. enableRequestEvents(Enable := true) {
  77. static vtable := init_vtable()
  78. if !Enable
  79. return this._ievents := this._ref := 0
  80. if this._ievents
  81. return
  82. IConnectionPointContainer := ComObjQuery(pwhr := ComObjValue(this.whr), '{B196B284-BAB4-101A-B69C-00AA00341D07}')
  83. DllCall('ole32\CLSIDFromString', 'str', '{F97F4E15-B787-4212-80D1-D380CBBF982E}', 'ptr', IID_IWinHttpRequestEvents := Buffer(16))
  84. ComCall(4, IConnectionPointContainer, 'ptr', IID_IWinHttpRequestEvents, 'ptr*', IConnectionPoint := ComValue(0xd, 0)) ; IConnectionPointContainer->FindConnectionPoint
  85. IWinHttpRequestEvents := Buffer(3 * A_PtrSize)
  86. NumPut('ptr', vtable.Ptr, 'ptr', ObjPtr(this), 'ptr', ObjPtr(IWinHttpRequestEvents), IWinHttpRequestEvents)
  87. ComCall(5, IConnectionPoint, 'ptr', IWinHttpRequestEvents, 'uint*', &dwCookie := 0) ; IConnectionPoint->Advise
  88. this._ievents := { __Delete: (*) => ComCall(6, IConnectionPoint, 'uint', dwCookie) }
  89. static init_vtable() {
  90. vtable := Buffer(A_PtrSize * 7), offset := vtable.Ptr
  91. for nParam in StrSplit('3113213')
  92. offset := NumPut('ptr', CallbackCreate(EventHandler.Bind(A_Index), , Integer(nParam)), offset)
  93. vtable.DefineProp('__Delete', { call: __Delete })
  94. return vtable
  95. static EventHandler(index, this, arg1 := 0, arg2 := 0) {
  96. if (index < 4) {
  97. IEvents := NumGet(this, A_PtrSize * 2, 'ptr')
  98. if index == 1
  99. NumPut('ptr', this, arg2)
  100. if index == 3
  101. ObjRelease(IEvents)
  102. else ObjAddRef(IEvents)
  103. return 0
  104. }
  105. req := ObjFromPtrAddRef(NumGet(this, A_PtrSize, 'ptr'))
  106. req.readyState := index - 2
  107. switch index {
  108. case 4: ; OnResponseStart
  109. try req.OnResponseStart(arg1, StrGet(arg2, 'utf-16'))
  110. case 5: ; OnResponseDataAvailable
  111. try req.OnResponseDataAvailable(
  112. NumGet((pSafeArray := NumGet(arg1, 'ptr')) + 8 + A_PtrSize, 'ptr'),
  113. NumGet(pSafeArray + 8 + A_PtrSize * 2, 'uint'))
  114. case 6: ; OnResponseFinished
  115. try req._ref := 0, req.OnResponseFinished()
  116. case 7: ; OnError
  117. try req.readyState := req._ref := 0, req.OnError(arg1, StrGet(arg2, 'utf-16'))
  118. }
  119. }
  120. static __Delete(this) {
  121. loop 7
  122. CallbackFree(NumGet(this, (A_Index - 1) * A_PtrSize, 'ptr'))
  123. }
  124. }
  125. }
  126. ;#region IWinHttpRequest https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-interface
  127. SetProxy(ProxySetting, ProxyServer, BypassList) => this.whr.SetProxy(ProxySetting, ProxyServer, BypassList)
  128. SetCredentials(UserName, Password, Flags) => this.whr.SetCredentials(UserName, Password, Flags)
  129. SetRequestHeader(Header, Value) => this.whr.SetRequestHeader(Header, Value)
  130. GetResponseHeader(Header) => this.whr.GetResponseHeader(Header)
  131. GetAllResponseHeaders() => this.whr.GetAllResponseHeaders()
  132. Send(Body?) => (this._ievents && this._ref := this, this.whr.Send(Body?))
  133. Open(verb, url, async := false) {
  134. this.readyState := 0
  135. this.whr.Open(verb, url, async)
  136. this.readyState := 1
  137. }
  138. WaitForResponse(Timeout := -1) => this.whr.WaitForResponse(Timeout)
  139. Abort() => (this._ref := this.readyState := 0, this.whr.Abort())
  140. SetTimeouts(ResolveTimeout := 0, ConnectTimeout := 60000, SendTimeout := 30000, ReceiveTimeout := 30000) => this.whr.SetTimeouts(ResolveTimeout, ConnectTimeout, SendTimeout, ReceiveTimeout)
  141. SetClientCertificate(ClientCertificate) => this.whr.SetClientCertificate(ClientCertificate)
  142. SetAutoLogonPolicy(AutoLogonPolicy) => this.whr.SetAutoLogonPolicy(AutoLogonPolicy)
  143. Status => this.whr.Status
  144. StatusText => this.whr.StatusText
  145. ResponseText => this.whr.ResponseText
  146. ResponseBody {
  147. get {
  148. pSafeArray := ComObjValue(t := this.whr.ResponseBody)
  149. pvData := NumGet(pSafeArray + 8 + A_PtrSize, 'ptr')
  150. cbElements := NumGet(pSafeArray + 8 + A_PtrSize * 2, 'uint')
  151. return ClipboardAll(pvData, cbElements)
  152. }
  153. }
  154. ResponseStream => this.whr.responseStream
  155. Option[Opt] {
  156. get => this.whr.Option[Opt]
  157. set => (this.whr.Option[Opt] := Value)
  158. }
  159. Headers {
  160. get {
  161. m := Map(), m.Default := ''
  162. loop parse this.GetAllResponseHeaders(), '`r`n'
  163. if (p := InStr(A_LoopField, ':'))
  164. m[SubStr(A_LoopField, 1, p - 1)] .= LTrim(SubStr(A_LoopField, p + 1))
  165. return m
  166. }
  167. }
  168. /**
  169. * The OnError event occurs when there is a run-time error in the application.
  170. * @prop {(this,errCode,errDesc)=>void} OnError
  171. */
  172. OnError := 0
  173. /**
  174. * The OnResponseDataAvailable event occurs when data is available from the response.
  175. * @prop {(this,safeArray)=>void} OnResponseDataAvailable
  176. */
  177. OnResponseDataAvailable := 0
  178. /**
  179. * The OnResponseStart event occurs when the response data starts to be received.
  180. * @prop {(this,status,contentType)=>void} OnResponseDataAvailable
  181. */
  182. OnResponseStart := 0
  183. /**
  184. * The OnResponseFinished event occurs when the response data is complete.
  185. * @prop {(this)=>void} OnResponseDataAvailable
  186. */
  187. OnResponseFinished := 0
  188. ;#endregion
  189. readyState := 0, whr := 0, _ievents := 0
  190. static __New() {
  191. if this != WinHttpRequest
  192. return
  193. this.DeleteProp('__New')
  194. for prop in ['OnError', 'OnResponseDataAvailable', 'OnResponseStart', 'OnResponseFinished']
  195. this.Prototype.DefineProp(prop, { set: make_setter(prop) })
  196. make_setter(prop) => (this, value := 0) => value && (this.DefineProp(prop, { call: value }), this.enableRequestEvents())
  197. }
  198. }

附:

用Apifox访问,也正常返回

AHK v1 官方示例源代码

  1. whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
  2. whr.Open("GET", "https://www.autohotkey.com/download/1.1/version.txt", true)
  3. whr.Send()
  4. whr.WaitForResponse()
  5. version := whr.ResponseText
  6. MsgBox % version

AHK v1运行失败报错 0x80092004 找不到对象或属性

用Msxml2.XMLHTTP则能正常得到结果

  1. #Requires AutoHotkey v2.0
  2. req := ComObject("Msxml2.XMLHTTP")
  3. req.open("GET", "https://www.autohotkey.com/download/2.0/version.txt", true)
  4. req.onreadystatechange := Ready
  5. req.send()
  6. Persistent
  7. Ready() {
  8. if (req.readyState != 4) ; 没有完成.
  9. return
  10. if (req.status == 200) ; OK.
  11. MsgBox "Latest AutoHotkey version: " req.responseText
  12. else
  13. MsgBox "Status " req.status,, 16
  14. ExitApp
  15. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/848087
推荐阅读
相关标签
  

闽ICP备14008679号