vba如何读取和存储LTPA?

时间:2018-09-18 09:05:08

标签: excel vba cookies xmlhttprequest winhttp

我正在尝试使用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

我为我的英语不好和我犯的“严重错误”表示歉意。

0 个答案:

没有答案