如何向可能重定向到登录页面的页面发出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.
我试图检测是否显示登录页面,如果显示,则将用户名和密码作为表单变量发布(我希望这等同于人类在Web浏览器的页面中键入所述用户名和密码).
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).
步骤如下:
*请求网址(包括带有帖子的文件).
*检查响应是否为登录页面.
*如果是这样,则在同一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无疑是很久以前开发的.我只是度过了三个晚上的大部分时间,因为同样的问题而感到沮丧,当我停止研究更多内容时,这个话题就不断出现,所以我想我会为下一个出现的人做出贡献. 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. 诀窍是: 我确定还有其他方法,但这对我来说似乎很优雅,我发现了很多其他使用方法. 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 var,它必须在打开连接后出现(在任何地方都看不到,只是无法让它坚持下去)封闭的连接.) 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). 下一个家伙"祝你好运! Good luck "next guy"! 我发现在MSDN迷宫中有用的一些信息. Some info I found useful in the MSDN maze. 这篇关于如何向可能重定向到登录页面的页面发出POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
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