我正在尝试通过 VB.Net 中的 Web 请求登录网站(“https://instagram.com/”)。我的凭据是正确的,并且我正确获取了所需的令牌,但是在登录时返回了 403 HTTP 错误。我当前的代码,包括我的 POST/GET 函数,可以在下面找到。
POST_ERROR:远程服务器返回错误:(403)禁止。
代码:
Imports System.Net
Imports System.IO
Imports System.Text
Module Main
Dim Token$ = ""
Dim myCookie As New CookieContainer
Private Sub UpdateToken(ByVal Page$)
Token = GetBetween(Page, "csrfmiddlewaretoken" & Chr(34) & " value=" & Chr(34), Chr(34) & "/>")
End Sub
Private Function GETreq(ByVal URL$)
Try
Dim tempCookie As New CookieContainer
Dim Request As HttpWebRequest = TryCast(WebRequest.Create(URL), HttpWebRequest)
Request.Method = "GET"
Request.CookieContainer = myCookie
Request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0"
Request.KeepAlive = True
Dim Response As HttpWebResponse = Request.GetResponse()
Dim ResponseStream As Stream = Response.GetResponseStream()
Dim StreamReader As New StreamReader(ResponseStream)
Dim Text$ = StreamReader.ReadToEnd()
tempCookie.Add(Response.Cookies)
myCookie = tempCookie
Return Text
Catch ex As Exception
Console.ForegroundColor = ConsoleColor.Gray
Console.WriteLine("GET_ERROR: " & ex.Message)
Return ""
End Try
End Function
Private Function POSTreq(ByVal URL$, ByVal Data$)
Try
Dim tempCookie As New CookieContainer
Dim DataBytes As Byte() = Encoding.ASCII.GetBytes(Data)
Dim Request As HttpWebRequest = TryCast(WebRequest.Create(URL), HttpWebRequest)
Request.Method = "POST"
Request.ContentType = "application/x-www-form-urlencoded"
Request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0"
Request.ContentLength = DataBytes.Length
Request.CookieContainer = myCookie
Request.KeepAlive = True
Dim PostData As Stream = Request.GetRequestStream()
PostData.Write(DataBytes, 0, DataBytes.Length)
PostData.Close()
Dim Response As HttpWebResponse = Request.GetResponse()
Dim ResponseStream As Stream = Response.GetResponseStream()
Dim StreamReader As New StreamReader(ResponseStream)
Dim Text$ = StreamReader.ReadToEnd()
tempCookie.Add(Response.Cookies)
myCookie = tempCookie
Return Text
Catch ex As Exception
Console.ForegroundColor = ConsoleColor.Gray
Console.WriteLine("POST_ERROR: " & ex.Message)
Return ""
End Try
End Function
Public Function GetBetween(ByRef strSource$, ByRef strStart$, ByRef strEnd$, Optional ByRef startPos As Integer = 0) As String
Try
Dim iPos As Integer, iEnd As Integer, lenStart As Integer = strStart.Length
Dim strResult$
strResult = String.Empty
iPos = strSource.IndexOf(strStart, startPos)
iEnd = strSource.IndexOf(strEnd, iPos + lenStart)
If iPos <> -1 AndAlso iEnd <> -1 Then
strResult = strSource.Substring(iPos + lenStart, iEnd - (iPos + lenStart))
End If
Return strResult
Catch ex As Exception
Return ""
End Try
End Function
Sub Main()
Dim Username$ = "XXX"
Dim Password$ = "XXX"
UpdateToken(GETreq("https://instagram.com/accounts/login/"))
UpdateToken(POSTreq("https://instagram.com/accounts/login/", String.Format("csrfmiddlewaretoken={0}&username={1}&password={2}", Token, Username, Password)))
Console.ReadLine()
End Sub
End Module