我正在尝试对需要 POST 登录身份验证的网站进行屏幕截图。我可以在我的第一个请求中很好地进行身份验证,但是当我尝试转身并点击下一页时,我会重定向回登录页面(基本上是说我没有登录)。
代码:
Public Function GetPage(ByVal PageName As String, ByVal UserName As String, ByVal Password As String) As String
Dim ReturnString As String = ""
Dim Cookies As New CookieContainer
Dim AuthURI As Uri = New Uri(AuthURL)
Cookies.GetCookieHeader(AuthURI)
Cookies.GetCookies(AuthURI)
'Set Header/Meta Info
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
Dim request As HttpWebRequest = HttpWebRequest.Create(AuthURL)
request.Method = "POST"
request.CookieContainer = Cookies
request.UserAgent = "Mozilla/5.0 (Windows; U;Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"
'Set POST Info
Dim postData As String = "userName=" & HttpUtility.UrlEncode(UserName) & "&password=" & HttpUtility.UrlEncode(Password)
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
'Write to the request stream
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
' Get the response.
Dim response As HttpWebResponse = request.GetResponse()
dataStream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
ReturnString = responseFromServer
'Append cookie data
For Each c As Cookie In response.Cookies
Cookies.Add(c)
Next
' Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
'Bail on fail
If ReturnString.Contains("Login failed") Then Return Nothing
'Generate new request
request = HttpWebRequest.Create(URLStub & PageName)
request.Method = "POST"
request.UserAgent = "Mozilla/5.0 (Windows; U;Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"
request.CookieContainer = Cookies
'Set POST Info
postData = "userName=" & HttpUtility.UrlEncode(UserName) & "&password=" & HttpUtility.UrlEncode(Password)
byteArray = Encoding.UTF8.GetBytes(postData)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
'Write to the request stream
dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
'Get the response.
response = request.GetResponse
dataStream = response.GetResponseStream
reader = New StreamReader(dataStream)
responseFromServer = reader.ReadToEnd
ReturnString = responseFromServer
'Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
Return ReturnString
End Function
这段代码模仿了另一个用 PHP 编写的页面。PHP 代码页可以正常工作,而且我使用的 URL 与 PHP 版本相同,因此我有 99% 的把握确定问题不在服务器端。
我也搜索过关于这个问题的其他帖子,看来我在语法方面做的一切都是正确的,但也许我错过了一些小而愚蠢的东西?
有任何想法吗?我已经为此奋斗了几天了。先感谢您。:)
编辑:我一直在使用会话 cookie,并且一直在比较初始登录和实际请求。这是我所拥有的:
FIRST:
ORA_WX_SESSION: "1FAA2AB1EF40DF4BC291DD3326F1DC3C596F56CF-2#3"
JSESSIONID: a2c38b128e1e54051a2c95c5a3a1e3a4cb0cb5b7ba74cd260aaec531856d722f.e34SahmMbNaMe34Sa3yPaN8Sc40
XYZCustomerServiceUserName: (login-name)
XYZ: d53f8dcd87b861a61d99ac21ec53bb2b
LAST:
ORA_WX_SESSION: 1FAA2AB1EF40DF4BC291DD3326F1DC3C596F56CF-2#3
JSESSIONID: a2c38b128e1e54051a2c95c5a3a1e3a4cb0cb5b7ba74cd260aaec531856d722f.e34SahmMbNaMe34Sa3yPaN8Sc40
我注意到两件事……首先, ORA_WX_SESSION 的引号被从其中剥离(即使我试图在响应和请求之间强制它们返回)。XYZCustomerServiceUserName 和 XYZ 值也被剥离。除此之外,两次连接尝试之间的会话信息是相同的。