excel - 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
我为我糟糕的英语和我所犯的“可怕的错误”道歉。
解决方案
推荐阅读
- angular - 如何获取角度材料多选字段的值和文本
- kotlin - Kotlin 无法转换为整数
- azure-cognitive-services - ComputerVisionErrorException:操作返回了无效的状态代码“未授权”
- laravel - 忽略 PHPUnit 9 中的弃用通知?
- asp.net-core - 如何在 Entity Framework Core 中建立一对多关系
- php - 使用 mysqli bind_param() 查询 WHERE 子句不起作用
- kendo-ui - 剑道网格弹出编辑器 - Razor ForEach - 列表为空
- spring - 按自定义函数 HIBERNATE 的结果分组
- c# - 列表上的布尔值给出不稳定的结果
- arrays - 从文件/目录位置的 JSON 数据创建一个数组;如果元素是目录,bash 不注册