首页 > 解决方案 > vba如何读取和存储LTPA?

问题描述

我正在尝试使用 WinHTTP 或 xmlHTTP 从 Excel 表单到我们的 Intranet 建立“静默”连接。

我刚刚编写了打开 IE、登录、传递数据、编译表单并获取我想要的信息的代码。现在我想在不打开 IE 会话的情况下做同样的事情。

我已经编写了一些代码,我达到了登录并“导航”到下一页的目标,但我无法继续。我认为这是 LTPA 令牌的问题。我之前在论坛上问过一个更通用的帖子。

这是我的代码

' 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

我为我糟糕的英语和我所犯的“可怕的错误”道歉。

标签: excelvbacookiesxmlhttprequestwinhttp

解决方案


推荐阅读