使用vbscript / java在NTLM身份验证后自动下载动态生成的文件 [英] Automate download of a dynamically generated file after NTLM Authentication using vbscript/java
问题描述
我正在尝试自动化在服务器上下载动态生成的文件,所以这里是我必须手动执行的步骤 -
I am trying to automate to download a dynamically generated file on a server, so here are the steps what I have to carry out manually -
- 登录后填写表单中的详细信息,使用post方法将详细信息发送到服务器,并生成一个文件在服务器上并恢复对服务器的响应。
所以我首先想到使用VBScript:
So first I thought of doing it using VBScript:
-
获取IE自动对象
Get a IE Automation object
sendkeys发送userword / password
sendkeys to send userword/password
浏览该页面并下载文件
Sendkeys切换到保存按钮 - 但在这里我被卡住了,因为IE提示我保存位置,我无法确定。另外我也想打开它,但是我无法通过这种方式打开Excel文件的自动化对象:(
Sendkeys to switch to save button - But here i got stuck because IE prompts me for save location which I can't determine. Also I thought of opening it but I am not able to get the automation object of Excel file opened this way :(
甚至 wget
无法正常工作,因为我必须发布一些数据和基础,它将为我提供最终的Excel文件。
Even wget
can't work as I have to post some data and basis of what it will provide me the resultant Excel file.
所以在网上搜索后,我发现我可以使用 MSXML2.xmlhttp
对象或Java套接字,并使用get方法下载页面,但是我必须提供我的凭据打开页面。
So after searching on web I found I can do it using MSXML2.xmlhttp
object or Java sockets and download the page using get method, but again I have to provide my credentials to open the page.
所以请任何人帮助我如何验证用户并以任何方式下载文件。
So can anybody please help me how to authenticate the user and download the file in either way.
编辑代码
- 下载页面
objmsXML.Open "GET", url, False
objmsXML.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objmsXML.setRequestHeader "Referer", url
objmsXML.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36"
objmsXML.send
- 填写细节和帖子数据
--Fill in the details and post data
If objmsXML.Status = 200 Then
'First Response received.
'get all the response headers
responseHeaders = objmsXML.getAllResponseHeaders()
responseBody = objmsXML.responseText
End If
Dim viewState As String
Dim eventTarget, eventArgument, txtProjectID, btnSubmit, grdReportPostDataValue
Dim eventValidation As String
viewState = Split(Split(responseBody, "__VIEWSTATE")(2), """")(2)
viewState = URLEncode(viewState, False)
eventTarget = ""
eventArgument = ""
txtProjectID = projectID
btnSubmit = URLEncode("Submit")
grdReportPostDataValue = ""
eventValidation = Split(Split(responseBody, "__EVENTVALIDATION")(2), """")(2)
eventValidation = URLEncode(eventValidation, False)
objmsXML.Open "POST", url, False
Dim postData
postData = "__EVENTTARGET=" & eventTarget & "&__EVENTARGUMENT=" _
& eventArgument & "&__VIEWSTATE=" & viewState & "&txtProjectID=" _
& txtProjectID & "&btnSubmit=" & btnSubmit & "&grdReportPostDataValue" _
& grdReportPostDataValue & "&__EVENTVALIDATION=" & eventValidation
objmsXML.send postData
但问题是它没有给作为表单结果的第二页。我相信这可能是因为我无法跟踪会话cookie。请帮助。
But the problem is it is not giving me the second page as a result of form post. I believe this might be because I am not able to track the session cookie. Please help.
推荐答案
您不能在不提供凭据的情况下验证用户。使用 Fiddler 进行会话检查时手动下载文件。这将显示 POST
请求所需的标题。然后自动执行请求:
You can't authenticate a user without providing credentials. Manually download the file once while inspecting the session with something like Fiddler. That will reveal the headers required for the POST
request. Then automate the request like this:
url = "http://..."
user = "..."
pass = "..."
credentials = "username=" & user & "&password=" & pass
Set req = CreateObject("Msxml2.XMLHttp.6.0")
req.open "POST", url, False
req.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.send credentials
修改<$根据Fiddler透露,c $ c>凭据您可能需要在以下示例中对值( user
和 pass
进行编码):
Modify credentials
according to what Fiddler revealed. You may need to encode the values (user
and pass
in the example above) with something like this:
Function Encode(ByVal str)
Set re = New RegExp
re.Pattern = "[^a-zA-Z0-9_.~-]"
enc = ""
For i = 1 To Len(str)
c = Mid(str, i, 1)
If re.Test(c) Then c = "%" & Right("0" & Hex(Asc(c)), 2)
enc = enc & c
Next
Encode = enc
End Function
将 responseBody
保存到这样的文件中:
Save the responseBody
to a file like this:
filename = "C:\your\output.xls"
Set stream = CreateObject("ADODB.Stream")
If req.status = 200 Then
stream.Open
stream.Type = 1 'binary
stream.Write req.responseBody
stream.SaveToFile filename, 2
stream.Close
End If
如果文件名是动态生成的,您可能需要阅读响应和/或状态文本并发送第二个请求来实际检索文件。然而,这取决于实际的服务器响应(由Fiddler透露)。
If the filename is dynamically generated you may need to read the response and/or status text and send a second request to actually retrieve the file. However, that depends on the actual server response (as revealed by Fiddler).
编辑:我发现一个解决方案这里建议使用 WinHttpRequest
对象:
A solution I found here suggests using a WinHttpRequest
object:
Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
req.SetAutoLogonPolicy 0
req.Open "POST", url, False
req.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
req.Send credentials
然而,我无法测试这个,因为我手头没有使用NTLM认证的Web服务器。
I can't test this, though, because I don't have a web server using NTLM authentication at hand.
这篇关于使用vbscript / java在NTLM身份验证后自动下载动态生成的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!