VB.net 中的 HttpWebRequests 和 Cookie
HttpWebRequests and Cookies in VB.net
所以我在 vb.net 中尝试使用 httpwebrequests 登录网站时遇到了一些问题。该站点没什么了不起的,因此绝对可以登录。但我遇到的问题是 cookie 容器。一个人告诉我不要担心 cookie 并且不要包含它们,而其他人告诉我只需创建一个变量作为新的 cookiecontainer 它将存储来自站点的所有 cookie,而无需将它们放入手动。但是为了登录站点,post 数据需要存储在 cookie 中的 csrftoken。但是,它也可以从源代码中删除。但是我想知道 cookie 是如何从我尝试登录到我的 cookie 容器的站点获取的,然后我才能登录,这样我就可以在 post 数据中使用 cookie 中的 csrftoken,这样我就可以登录在?
这是我的代码:(注意我抓取了 csrftoken,因为我不知道如何从容器中获取它)
Imports System.Net
Imports System.Text
Imports System.IO
Imports HtmlAgilityPack
Public Class Form1
Dim csrftoken As String
Dim logincookie As New CookieContainer
Dim uri As Uri = New Uri("site")
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sourceString As String = New System.Net.WebClient().DownloadString("site")
Dim doc As New HtmlDocument
doc.LoadHtml(sourceString)
For Each node As HtmlAgilityPack.HtmlNode In doc.DocumentNode.SelectNodes("//*[@id=""login_inputs""]/form/input[1]")
Dim csrftokenmixed As String = node.OuterHtml
Dim cutat As String = "value='"
Dim x As Integer = InStr(csrftokenmixed, cutat)
Dim csrftoken1 As String = csrftokenmixed.Substring(x + cutat.Length - 1)
Dim cutat2 As String = "'"
Dim x2 As Integer = InStr(csrftoken1, cutat2)
csrftoken = csrftoken1.Substring(0, x2 - 1)
Next
'请注意,我知道这是非常糟糕的编码,但我只是在测试它,我只是复制并粘贴了一些代码以获得值。
Dim username As String = TextBox1.Text
Dim password As String = TextBox2.Text
Dim postData As String = "next=&csrfmiddlewaretoken=" & csrftoken & "&username=" & username & "&password=" & password
Dim tempCookies As New CookieContainer
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("site"), HttpWebRequest)
postReq.Method = "POST"
postReq.Host = "site.com"
postReq.KeepAlive = True
postReq.ContentLength = byteData.Length
postReq.Headers.Add("Cache-Control", "max-age=0")
postReq.Headers.Add("Origin", "site")
postReq.Headers.Add("Upgrade-Insecure-Requests", "1")
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
postReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
postReq.Referer = "site"
postReq.Headers.Add("Accept-Encoding", "gzip, deflate, br")
postReq.Headers.Add("Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6")
*tempCookies*.Add(uri, New Cookie("csrftoken", csrftoken))
postReq.CookieContainer = tempCookies
Dim postreqstream As Stream = postReq.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
For Each tempCookie In *tempCookies*.GetCookies(uri)
Debug.Print(tempCookie.name & " = " & tempCookie.value)
Next
这是代码在尝试获取响应时卡住的地方 - 错误 403 远程服务器返回错误 - 禁止。
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
logincookie = tempCookies
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
WebBrowser1.DocumentText = thepage
End Sub
结束Class
是的,我忘了说我实际上无法收到回复 - 我认为这是因为我的 cookie,但我不知道?感谢任何帮助我去哪里。
此外,在任何人说之前确保您所有的 headers 都是相同的 - 我有很多次,唯一认为我无法复制的是饼干。
更新:刚刚意识到我使用 logincookie 作为 cookie 容器而不是 tempCookies - 所以我基本上用代码第二部分下方的 tempCookies 替换了所有 logincookies。但是所有的数据看起来都是这样的:
简答:
csrftoken 可以在响应头中得到。
所以在执行 POST 之前,只需执行 GET,然后作为响应,您将获得并拥有 csrftoken ,然后在 POST 参数中使用它...
所以我在 vb.net 中尝试使用 httpwebrequests 登录网站时遇到了一些问题。该站点没什么了不起的,因此绝对可以登录。但我遇到的问题是 cookie 容器。一个人告诉我不要担心 cookie 并且不要包含它们,而其他人告诉我只需创建一个变量作为新的 cookiecontainer 它将存储来自站点的所有 cookie,而无需将它们放入手动。但是为了登录站点,post 数据需要存储在 cookie 中的 csrftoken。但是,它也可以从源代码中删除。但是我想知道 cookie 是如何从我尝试登录到我的 cookie 容器的站点获取的,然后我才能登录,这样我就可以在 post 数据中使用 cookie 中的 csrftoken,这样我就可以登录在? 这是我的代码:(注意我抓取了 csrftoken,因为我不知道如何从容器中获取它)
Imports System.Net
Imports System.Text
Imports System.IO
Imports HtmlAgilityPack
Public Class Form1
Dim csrftoken As String
Dim logincookie As New CookieContainer
Dim uri As Uri = New Uri("site")
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sourceString As String = New System.Net.WebClient().DownloadString("site")
Dim doc As New HtmlDocument
doc.LoadHtml(sourceString)
For Each node As HtmlAgilityPack.HtmlNode In doc.DocumentNode.SelectNodes("//*[@id=""login_inputs""]/form/input[1]")
Dim csrftokenmixed As String = node.OuterHtml
Dim cutat As String = "value='"
Dim x As Integer = InStr(csrftokenmixed, cutat)
Dim csrftoken1 As String = csrftokenmixed.Substring(x + cutat.Length - 1)
Dim cutat2 As String = "'"
Dim x2 As Integer = InStr(csrftoken1, cutat2)
csrftoken = csrftoken1.Substring(0, x2 - 1)
Next
'请注意,我知道这是非常糟糕的编码,但我只是在测试它,我只是复制并粘贴了一些代码以获得值。
Dim username As String = TextBox1.Text
Dim password As String = TextBox2.Text
Dim postData As String = "next=&csrfmiddlewaretoken=" & csrftoken & "&username=" & username & "&password=" & password
Dim tempCookies As New CookieContainer
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("site"), HttpWebRequest)
postReq.Method = "POST"
postReq.Host = "site.com"
postReq.KeepAlive = True
postReq.ContentLength = byteData.Length
postReq.Headers.Add("Cache-Control", "max-age=0")
postReq.Headers.Add("Origin", "site")
postReq.Headers.Add("Upgrade-Insecure-Requests", "1")
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
postReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
postReq.Referer = "site"
postReq.Headers.Add("Accept-Encoding", "gzip, deflate, br")
postReq.Headers.Add("Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6")
*tempCookies*.Add(uri, New Cookie("csrftoken", csrftoken))
postReq.CookieContainer = tempCookies
Dim postreqstream As Stream = postReq.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
For Each tempCookie In *tempCookies*.GetCookies(uri)
Debug.Print(tempCookie.name & " = " & tempCookie.value)
Next
这是代码在尝试获取响应时卡住的地方 - 错误 403 远程服务器返回错误 - 禁止。
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
logincookie = tempCookies
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
WebBrowser1.DocumentText = thepage
End Sub
结束Class
是的,我忘了说我实际上无法收到回复 - 我认为这是因为我的 cookie,但我不知道?感谢任何帮助我去哪里。
此外,在任何人说之前确保您所有的 headers 都是相同的 - 我有很多次,唯一认为我无法复制的是饼干。
更新:刚刚意识到我使用 logincookie 作为 cookie 容器而不是 tempCookies - 所以我基本上用代码第二部分下方的 tempCookies 替换了所有 logincookies。但是所有的数据看起来都是这样的:
简答:
csrftoken 可以在响应头中得到。
所以在执行 POST 之前,只需执行 GET,然后作为响应,您将获得并拥有 csrftoken ,然后在 POST 参数中使用它...