1

I am trying to pull website data using WinHttp.WinHttpRequest.5.1. The website requires login and the cookie is stored with IE11. WinHttp.WinHttpRequest.5.1 creates it's own instance and therefore is not logged in to the requested website. Is there any way to use the active cookie from IE11 in the WinHttp.WinHttpRequest.5.1 request?

myURL = "https://postman-echo.com/get?foo1=bar1&foo2=bar2"
Set oXMLHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
oXMLHttp.Open "GET", myURL , False
oXMLHttp.send
    ohtmlFile.Write oXMLHttp.responseText
    ohtmlFile.Close
  • Take a look at [Retrieve ALL cookies from Internet Explorer](https://stackoverflow.com/questions/38726408/retrieve-all-cookies-from-internet-explorer). In the answer, the cookies are retrieved from the text files in the IE folder. If you just need to login into the API, I suggest you pass the required credentials in your HTTP request directly. – Étienne Laneville Oct 08 '19 at 16:11

2 Answers2

1

You could try to use the WinHttpRequest GetResponseHeader method or parse the GetAllResponseHeaders method result to get the cookie value. Then, using the SetRequestHeader method to Adds, changes, or deletes an HTTP request header. More detail information, please check this article and this thread.

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • I don't think he has the cookie values on the initial request, that's what he is trying to get. He can't get them from a response because he can't place a request without the cookies. – Étienne Laneville Oct 08 '19 at 16:14
0

If the website requires a login, usually you can provide that information using HTTP Basic Authentication. This is usually done by adding a special header (Authorization) we add username:password encoded in base64.

Your code might look like this:

Dim sAuthorization
sAuthorization = "Basic Zm9vOmJhcg=="

oXMLHttp.Open "GET", myURL , False
oXMLHttp.setRequestHeader "Authorization", sAuthorization
oXMLHttp.send

This will depend on the API though, you can check the authentication requirements. It could use a different method and you might not need to encode your username:password. Sometimes you provide an API Key instead. Whatever the method used, you should be able to provide the required authentication in your HTTP Request.

Étienne Laneville
  • 4,697
  • 5
  • 13
  • 29