vba how to read and store LTPA?

2019-08-24 12:35发布

I'm trying to establish a "silent" connection using WinHTTP or xmlHTTP starting from an Excel form to our intranet.

I've just written code that opens IE, does login, passes data, compiles form and gets the information I want. Now I want to do the same without open a IE session.

I have written some code and I reach the aim to login and "navigate" to the next page but i can't go on. I think it's a problem with LTPA token. I asked before on the forum a more generic post.

Here is my code

' Takes a string of the form "JSESSIONID=40DD2DFCAF24A2D64544F55194FCE04E;path=/pamsservices;HttpOnly"
' and returns only the portion "JSESSIONID=40DD2DFCAF24A2D64544F55194FCE04E"
Public Function GetJsessionIdCookie(setCookieStr As String) As String
    'JSESSIONID=40DD2DFCAF24A2D64544F55194FCE04E;path=/pamsservices;HttpOnly

    Dim jsessionidCookie As String

    Dim words() As String
    Dim word As Variant

    words = Split(setCookieStr, ";")
    For Each word In words
        If InStr(1, word, "JSESSIONID") > 0 Then
            jsessionidCookie = word
        End If
    Next word

    GetJsessionIdCookie = jsessionidCookie
End Function

Public Sub Login()
    Dim JUserid, userid As String
    Dim JPassword, password As String

    userid = "xxxxxx"
    password = "yyyyyyy"
    JUserid = "j_username=" & userid
    JPassword = "j_password=" & password

    Dim postReq, getReq, cookies
    Dim p0 As Integer, p1 As Integer, temp As String
    Dim result As String, respHead As String
    Dim http As Object

    Set http = CreateObject("WinHttp.WinHttpRequest.5.1") 'initialize  other CreateObject("MSXML2.serverXMLHTTP.6.0") or 'CreateObject("MSXML2.serverXMLHTTP.6.0")

    http.Open "GET", "http://kkkkkkk.it/Home/Logon.jsp", False
    http.setRequestHeader "Content-Type", "ext/html; charset=ISO-8859-1"
    http.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    http.setRequestHeader "User-Agent", "MS Internet Explorer"
    http.send

    strCookie = http.getResponseHeader("Set-Cookie") ' --> "JSESSIONID=40DD2DFCAF24A2D64544F55194FCE04E;path=/pamsservices;HttpOnly"
    jsessionidCookie = GetJsessionIdCookie(strCookie)

    Debug.Print jsessionidCookie
    http.getAllResponseHeaders
    respHead = http.getAllResponseHeaders()
    'Debug.Print respHead
    'Debug.Print jsessionidCookie

    http.Open "GET", "http://KKKKKKKKK.it/Home/j_security_check", False
    http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    http.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    http.setRequestHeader "User-Agent", "MS Internet Explorer"

    http.send

    http.getAllResponseHeaders
    respHead = http.getAllResponseHeaders()
    Debug.Print http.responseText
    Debug.Print respHead

    http.Open "POST", "http://kkkkkkk.it/Home/j_security_check", False
    http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    http.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    http.setRequestHeader "User-Agent", "MS Internet Explorer"

    http.send JUserid & "&" & JPassword

    http.getAllResponseHeaders
    respHead = http.getAllResponseHeaders()

    http.Open "GET", "http://kkkkkk.it/Home/HomeServlet", False
    http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    http.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    http.setRequestHeader "User-Agent", "MS Internet Explorer"
    http.send                                    '"Ric=informativa&informativa=true"

    http.getAllResponseHeaders
    respHead = http.getAllResponseHeaders()
    Debug.Print http.responseText
    Debug.Print respHead

    http.Open "post", "http://kkkk.it/Home/HomeServlet", False
    http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    http.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    http.setRequestHeader "User-Agent", "MS Internet Explorer"
    http.send "Ric=informativa&informativa=true"

    respHead = http.getAllResponseHeaders
    Debug.Print http.responseText
    Debug.Print respHead

    ' this is the code that makes the error " Problems with authentication"
    http.Open "GET", "http://kkkkkkk.it/Uffici/WebServlet", False
    http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    http.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
    http.setRequestHeader "User-Agent", "MS Internet Explorer"
    ' http.setRequestHeader '"Set-Cookie", jsessionidCookie
    http.send
    respHead = http.getAllResponseHeaders
    Debug.Print http.responseText
End Sub

I apologize for my bad English and for the "terrible mistakes" I have made.

0条回答
登录 后发表回答