【发布时间】:2014-10-05 01:52:27
【问题描述】:
好的,我已经查看了堆栈溢出问题,但我没有找到任何可以 100% 回答我遇到的问题的内容。我目前正在为一个名为 e926.net 的网站进行自动登录,我他们处理登录的方式有问题。
更新问题:(https://stackoverflow.com/questions/26200801/httpwebrequest-cookies-and-authorization-tokens) 注意:关于这个问题仍有重要信息。
现在我正在使用这个代码:
Sub Client()
Dim webRequest As HttpWebRequest
Dim responseReader As StreamReader
Dim responseData As String
Dim postData As String = "authenticity_token=ubL5PfTzyrvhB%2Bb1A2MISb2IUZxsEadmZWc0z7Gs2qA%3D&url=&user%5Bname%5D=USERNAME&user%5Bpassword%5D=PASSWORD&user%5Broaming%5D=0"
Dim cookies As CookieContainer = New CookieContainer()
Dim requestWriter As StreamWriter
Dim strUrl As String = Nothing
Dim strRequestedHTML As String
Try
'get login page with cookies
strUrl = "https://e926.net/user/login"
webRequest = HttpWebRequest.Create(strUrl)
webRequest.AllowAutoRedirect = True
webRequest.CookieContainer = cookies
'recieve non-authenticated cookie
webRequest.GetResponse().Close()
'post form data to page
strUrl = "https://e926.net/user/authenticate"
webRequest = HttpWebRequest.Create(strUrl)
webRequest.AllowAutoRedirect = True
webRequest.Method = WebRequestMethods.Http.Post
webRequest.ContentType = "application/x-www-form-urlencoded"
'webRequest.CookieContainer = cookies
webRequest.ContentLength = postData.Length
requestWriter = New StreamWriter(webRequest.GetRequestStream)
requestWriter.Write(postData)
requestWriter.Close()
'recieve authenticated cookie
webRequest.GetResponse().Close()
'now we get the authenticated page
webRequest = HttpWebRequest.Create("https://e926.net/post")
webRequest.CookieContainer = cookies
responseReader = New StreamReader(webRequest.GetResponse.GetResponseStream())
responseData = responseReader.ReadToEnd()
responseReader.Close()
strRequestedHTML = responseData
My.Computer.Clipboard.SetText(strRequestedHTML)
Catch ex As Exception
MsgBox("<br />There was an error going to this site: " + strUrl + "<br> Error: " + ex.Message)
Return
End Try
End Sub
该代码是从另一个站点找到并修改的,不幸的是我忘记了链接,但它在我尝试过的前 5 个站点上运行良好。但是由于 E926 通过从“登录”页面获取详细信息来工作然后重定向到“身份验证”页面,似乎程序没有正确传输 cookie 以允许传输来自“登录”的信息。我一直在查看“提琴手 2”,以了解“cookie”和实际发送到页面的信息看起来像什么,而且很明显 cookie 丢失了。我仔细检查了“身份验证令牌”,据我所见,它似乎从未改变,但它是在“登录”页面上以隐藏形式提供的。
所以我的问题是,我如何传输 cookie 以使登录过程正常进行,这是我的问题还是其他原因导致了问题?
编辑: 来自 IE 的标头:https://docs.google.com/document/d/1UPf2vzPF11Q-i_3xSkZmdN1dfqFZX4ydyAZhVeQlh94/edit?usp=sharing
程序/该代码的标题:https://docs.google.com/document/d/1ltZo3JZLt4sZiwZBltfqqParb3H86qi62I0KZN5t_VE/edit?usp=sharing
【问题讨论】:
-
旁注:他们的登录系统完全不安全。
-
是的,我想过几次,这就是为什么当我必须使用该网站时,我使用过时的密码和用户名。
标签: vb.net cookies web login-script