I want to get cookie information from "https://www.kent-web.com/pwd/protect/enter.cgi" whose ID and PW is both "guest".
I write the following code refering "https://stackoverflow.com/questions/39709562/how-to-set-and-get-jsessionid-cookie-in-vba"
I tried following code however I couldn't get any cookie information.
Sub Test()
Dim sUrl, sRespHeaders, sRespText, aSetHeaders, aList
sUrl = "https://www.kent-web.com/pwd/protect/enter.cgi"
XmlHttpRequest "POST", sUrl, Array(), "id=guest&pw=guest&login=LOG+IN", sRespHeaders, sRespText'Sending ID, PW and LOG+IN
End Sub
Sub XmlHttpRequest(sMethod, sUrl, aSetHeaders, sPayload, sRespHeaders, sRespText)
Dim aHeader, strCookie
With CreateObject("MSXML2.ServerXMLHTTP")
.Open sMethod, sUrl, False
For Each aHeader In aSetHeaders
.SetRequestHeader aHeader(0), aHeader(1)
Next
.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.SetRequestHeader "Connection", "keep-alive"
.Send (sPayload)
sRespHeaders = .GetAllResponseHeaders'"set-cookie: CGISESSID=" can be obtained by browser. However the cookie value is empty via VBA
sRespText = .ResponseText
strCookie = .GetResponseHeader("Set-Cookie")
End With
End Sub
I can get cookie information by the following C# code, however I need to get it through VBA.
using System.IO;
using System.Net;
using System.Text;
static void Main(string[] args)
{
var url = "https://www.kent-web.com/pwd/protect/enter.cgi";
var req = (HttpWebRequest)WebRequest.Create(url);
var cookieContainer = new CookieContainer();
req.CookieContainer = cookieContainer;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string postData = "id=guest&pw=guest&login=LOG+IN";
var data = Encoding.ASCII.GetBytes(postData);
req.ContentLength = data.Length;
using (Stream stream = req.GetRequestStream())
{
stream.Write(data, 0, data.Length);
using (var res = (HttpWebResponse)req.GetResponse())
{
CookieCollection cookies = cookieContainer.GetCookies(new System.Uri(url));
}
}
}