0

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));
            }
        }
    }
  • possible duplicate of https://stackoverflow.com/questions/39709562/how-to-set-and-get-jsessionid-cookie-in-vba – SSlinky Jan 20 '23 at 08:17
  • Does this answer your question? [How to set and get JSESSIONID cookie in VBA?](https://stackoverflow.com/questions/39709562/how-to-set-and-get-jsessionid-cookie-in-vba) – SSlinky Jan 20 '23 at 08:17
  • What do you get in `sRespHeaders` ? – CDP1802 Jan 20 '23 at 13:35
  • My code is based on the "https://stackoverflow.com/questions/39709562/how-to-set-and-get-jsessionid-cookie-in-vba" which is equal to "How to set and get JSESSIONID cookie in VBA?". – HTTPRequest Jan 21 '23 at 06:12
  • sRespHeaders get the following information"Connection: keep-alive Date: Sat, 21 Jan 2023 06:10:30 GMT Transfer-Encoding: chunked Content-Type: text/html; charset=shift_jis Server: nginx Vary: Accept-Encoding". I don't know the reason why but cookie infrmation is not included in the information. – HTTPRequest Jan 21 '23 at 06:13
  • If you add `Debug.Print .GetResponseHeader("Content-Type")` and get text/html it will show the code is correct and the server response is the problem. – CDP1802 Jan 21 '23 at 16:22
  • "Content-Type: text/html; charset=shift_jis" is obtained. And I confirm the response text and it shows that the page is what I would like to access. I guess "MSXML2.ServerXMLHTTP" is not suitable for the case. – HTTPRequest Jan 21 '23 at 23:39

0 Answers0