1

Currently I am working on pulling data from webapplication using WinHttpRequest. I managed to succesfully perform various task, however I stumbled across weird problem. Some data is missing in .ResponseText. I've done the task manually and the responsebody is:

   <?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>

And when I do the same with VBA the answer is shorter:

 <?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>

It is like I am missing some depth from the response. Below code I am using.

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

Does anyone know what the issue is? Even though I found similar topics, there is no clear answer.

braX
  • 11,506
  • 5
  • 20
  • 33
KamilG
  • 71
  • 7
  • I found a part of an answer. Problem lays within response header: Transfer-encoded : chunked. Now the only thing needed is finding a way to get chunked data. – KamilG Jan 08 '18 at 14:38
  • have you solved this? I also want to receive chunks of data. I found setting async to False caused chunks to arrive but I am not sure I want to do this. More info at http://exceldevelopmentplatform.blogspot.com/2018/01/vba-winhttprequest-no-asynchronous.html – S Meaden Jan 27 '18 at 22:33
  • Hey, unfortunately I did not solve it using VBA. I switched to C# where that kind of issues are easy to solve. I will try method from your link. More info in the future – KamilG Jan 29 '18 at 08:01
  • I have tried method in that link, but it is similar to one tagged as answer. It only allows to show data separated in chunks. Even though it is a solution for presenting chunked data - it does not solve my issue, as still part of the server response is missing. – KamilG Jan 29 '18 at 12:27

1 Answers1

1

Browsing that type library there are some events to trap, OnResponseDataAvavilable , OnResponseStart, OnResponseFinished. Try these to see if they handle your chunking.

Some skeleton code, your request variable needs to be defined in a class module for the WithEvents keyword to compile

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
S Meaden
  • 8,050
  • 3
  • 34
  • 65
  • Hi Thanks for an answer! I managed to create a succesfull class module with working events. Even though the ones you provided triggers couple of times during 1 "POST" run the .responsebody and .responsetext gives me exactly same answer each time. Do you have any more ideas which could allow to push me forward? – KamilG Jan 09 '18 at 09:03
  • I have added some lines of code to be able to see the result of data response from server. Problem is even though this trick works - I get all "chunkes" of data separately it is just the data i am being able with .responsetext just splitted into smaller parts. I guess winhttp is not able to surpass "send" method finish after first chunk. Private Sub oREQ_OnResponseDataAvailable(Data() As Byte) ThisWorkbook.Worksheets("Test").Cells(Count, 2) = "RESPONSE DATA AVA" & BinaryToText(Data, "utf-8") Count = Count + 1 End Sub – KamilG Jan 09 '18 at 09:47