SinSoul
SinSoul

Reputation: 93

Excel VBA browser automation GET/POST method

I'm creating automation script for my work project to extract links after login.

I'm trying to login and get JSESSIONID, but unfortunately I always get JSESSIONID prior to login, therefore any next request just gets me links from login page.

When I login using browser manually, get JSESSIONID from cookies and add it to the script, then everything works as I want it to.

Please see the example code I use below

Dim http As WinHttp.WinHttpRequest
Dim jsessionidCookie As String
Set http = New WinHttpRequest
http.Open "POST", "mywebpage/login/", False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "Connection", "keep-alive"
http.send "userId=myusername&password=mypassword"
jsessionidCookie = http.getResponseHeader("Set-Cookie")
jsessionidCookie = Split(jsessionidCookie, ";")(0) ' the JSESSSIONID I get here is always wrong, it doesn't authorise me for any next requests
'Debug.Print jsessionIDCookie
If http.Status = 200 Then
    
    http.Open "GET", "mypage/page/2/", False
    http.setRequestHeader "Cookie", jsessionidCookie ' when I manually add JSESSIONID here which I manually extract from cookies in MS edge, everything works as intended
    http.send
    ' here goes script for extracting links, which I have no problems with
End If

I apologise I cannot provide any links to the webpage :( The webpage uses jQuery library and submit is being handled by JavaScript, if that helps with anything. Please see login.js module from the webpage:

$(document).ready(function() {
if ($("#userId").val() == "") {
    $("#userId").focus();
} else {
    $("#password").focus();
}
$("input").keypress(function(e) {

    switch (e.which) {

    case 13: // enter
        //$('[name="login"]').click();
        $("#login").submit();
        return false;
    default:
        break;

    }
});
});

My last case scenario is extracting JSESSIONID manually everytime I need automation to work, it's not a big deal for me I guess, but explaining it to people who will be working with the app, who think, that VLOOKUP is hacking software and to explain them how to get JSESSIONID from browser would be really hard

I tried what was written here here and here I couldn't understand if wireshark would assist in my situation and I couldn't really understand what to do with it. Besides the website with ignoring the encryption doesn't open

Upvotes: 0

Views: 50

Answers (0)

Related Questions