0

目前我正在使用 WinHttpRequest 从 web 应用程序中提取数据。我设法成功地执行了各种任务,但是我偶然发现了奇怪的问题。.ResponseText 中缺少一些数据。我已经手动完成了任务,responsebody 是:

   <?xml version="1.0" encoding="UTF-8"?>
 <mxRoot><rows><r ra="t" o="20336.41905.48904.52482" p="" id="0,0" level="0" filter="false" t="1515418285649" r="" d="" hc="">
 <c a="91664964" i="images/iconSmallPart.png">91664964</c><c a="001">001</c> :ConfidentialInformations: </rows>

当我对 VBA 做同样的事情时,答案会更短:

 <?xml version="1.0" encoding="UTF-8"?>
 <mxRoot><rows><r ra="t" o="20336.41905.39945.55654" p="" id="0,0" level="0" filter="false" /></rows>

就像我从响应中遗漏了一些深度。下面的代码我正在使用。

Set MyRequest = CreateObject("WinHTTP.WinHTTPrequest.5.1")

MyRequest.Open "POST", URL

MyRequest.SetRequestHeader "Host", "plmprod.pg.com"
MyRequest.Option(WinHttpRequestOption_EnableRedirects) = False
MyRequest.SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:57.0) Gecko/20100101 Firefox/57.0"
MyRequest.SetRequestHeader "Accept", "*/*"
MyRequest.SetRequestHeader "Accept-Language", "pl,en-US;q=0.7,en;q=0.3"
MyRequest.SetRequestHeader "Accept-Encoding", "gzip, deflate, br"
MyRequest.SetRequestHeader "Referer", "RefererURL"
MyRequest.SetRequestHeader "Content-Type", "undefined"
MyRequest.SetRequestHeader "charset", "UTF-8"
MyRequest.SetRequestHeader "csrfTokenName", "ENO_CSRF_TOKEN"
MyRequest.SetRequestHeader "ENO_CSRF_TOKEN", CSRFTOKEN
MyRequest.SetRequestHeader "Cookie", "testcookie=1; " & JSESSION & "; " & SERVERID
MyRequest.SetRequestHeader "Connection", "keep-alive"
MyRequest.SetRequestHeader "Content-Length", "0"
MyRequest.Send
MyRequest.WaitForResponse
Debug.Print MyRequest.ResponseText

有谁知道问题是什么?即使我找到了类似的主题,也没有明确的答案。

4

1 回答 1

1

浏览该类型库,有一些事件要捕获 ,, OnResponseDataAvavilable, 。试试这些,看看它们是否能处理你的分块。OnResponseStartOnResponseFinished

一些骨架代码,你的请求变量需要在类模块中定义,以便WithEvents关键字编译

Option Explicit

Private WithEvents oReq As WinHttp.WinHttpRequest

Private Sub oReq_OnResponseDataAvailable(Data() As Byte)

    Dim sThisChunk As String
    sThisChunk = StrConv(Data(), vbUnicode)

End Sub

Private Sub oReq_OnResponseFinished()

End Sub

Private Sub oReq_OnResponseStart(ByVal Status As Long, ByVal ContentType As String)

End Sub
于 2018-01-08T16:18:56.123 回答