如何向可能重定向到登录页面的页面发出 POST 请求 [英] How to make a POST request to a page that may redirect to a login page
问题描述
我在 Outlook VBA 中使用宏通过 POST 向 URL 提交文件:
I am using a macro in Outlook VBA to submit a file via POST to a URL:
Set http = New WinHttp.WinHttpRequest
http.Open "POST", UrlToPostTo, False 'True '
http.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
http.setRequestHeader "Content-Type", "multipart/form-data; "
http.Send data
我的问题是接受请求的页面(在本例中为文件上传页面)受身份验证保护 - 上面的初始请求将返回登录页面而不是页面本身.
My problem is the page that will accept the request (a file upload page, in this case) is protected by authentication - the initial request for it above will return a login page instead of the page itself.
我试图检测登录页面是否出现,如果出现,将用户名和密码作为表单变量发布(我希望这相当于人类在网络浏览器的页面中输入用户名和密码).
I have tried to detect if the login page appears and if so, post the username and password as form variables (I'm hoping this is equivalent to a human typing said username and password into a page in the web browser).
所以步骤是:
* 请求 URL(包含带有帖子的文件).
* 检查响应是否为登录页面.
* 如果是,则在同一个http会话中,提交用户名和密码到URL.
* 如果服务器现在处理原始帖子,很好,否则我可以再发一次.
So the steps are:
* request URL (include file with post).
* Check if the reponse is the login page.
* If so, then in the same http session, submit the username and password to the URL.
* If the server now processes the original post, good, otherwise I can post it again.
代码如下:
' if the login page comes back, send credentials '
If (InStr(http.ResponseText, "j_password") > 0) Then
Dim loginData As String
loginData = "j_username=theusername&j_password=thepassword"
http.Open "POST", UrlToPostTo, False
http.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
http.setRequestHeader "Content-Type", "multipart/form-data; "
http.Send loginData
End If
但是当我这样做时,http.Responsetext 仍然只是登录页面(或再次?).
But when I do this, The http.Responsetext is just the login page still (or again?).
知道我做错了什么吗?我的计划是否有效?
Any idea what I'm doing wrong? Is my plan even valid?
(这与试图解决这个问题 )
推荐答案
我知道这个话题很古老,而且我意识到 OP 肯定在很久以前就已经移动了.我只是在 3 个晚上的大部分时间里都被这个完全相同的问题所困扰,当我停下来进行更多研究时,这个线程不断出现,所以我想我会为下一个出现的人做出贡献.
I know this thread is ancient and I realize the OP most definitely moved on long ago. I just spent the better part of 3 evenings being humbled by this exact same problem, and this thread kept coming up when I would stop to research more so I thought I would contribute for the next guy who comes along.
诀窍是:
- 使用禁用重定向Option 属性,阻止它在没有你的情况下继续前进.(见下面的评论)
- 捕获 Status 属性,以及从那里处理.
- Disable redirects using using the Option property, to stop it from moving on without you. (see comments below)
- Capture the redirect on the output of the Status property, and handle from there.
我敢肯定还有其他方法,但这对我来说似乎很优雅,而且我发现了很多其他方法来使用它.
I'm sure there's other ways, but this seemed pretty elegant to me, and I found lots of other ways to use it.
要使用 OPs 代码示例,您可以按计划发出请求,但有一个例外:EnableRedirects 变量,它必须在打开连接后出现(没有在任何地方读取,只是无法让它坚持关闭连接).
To use the OPs code example, you could make your request as planned with one exception: The EnableRedirects var, which must come after opening the connection (didn't read that anywhere, just couldn't get it to stick to a closed connection).
祝下一个人"好运!
Dim http As WinHttp.WinHttpRequest
Dim UrlToPostTo As String, UrlRedirectedTo As String
'Your initial request (assuming lots here)
Set http = New WinHttp.WinHttpRequest
http.Open "POST", UrlToPostTo, False
'Stop it from redirecting automatically, so you can capture it
http.Option(WinHttpRequestOption_EnableRedirects) = False 'You can also use the collection index instead of the pretty name
http.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
http.setRequestHeader "Content-Type", "multipart/form-data; "
http.Send
'Now if you have an active session, you should get your desired content
'If it redirected you, you'll have a different status, header etc...
If http.status = "302" Then
Dim loginData As String
'Now lets find out where we're being pointed and POST there
'This may not be the same url you see in your address bar
UrlRedirectedTo = http.GetResponseHeader("Location")
'Also, you may have to do this again to arrive back at the intended resource
loginData = "j_username=theusername&j_password=thepassword"
http.Open "POST", UrlRedirectedTo, False
http.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
http.setRequestHeader "Content-Type", "multipart/form-data; "
http.Send loginData
End If
我在 MSDN 迷宫中发现一些有用的信息.
Some info I found useful in the MSDN maze.
这篇关于如何向可能重定向到登录页面的页面发出 POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!