HttpWebRequest Login Error 403. Kann keinen Fehler finden!

  • VB.NET

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von petaod.

    HttpWebRequest Login Error 403. Kann keinen Fehler finden!

    Hey,

    Ich versuch mich mittels Httpwebrequest bei Instagram anzumelden, also eigtl. nach einem stinknormalem Httpwebrequest-Schema.
    Hier der Code mit ersetzten Benutzerangaben:

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Dim logincookie As CookieContainer
    2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3. Dim postData As String = "csrfmiddlewaretoken=d5012da15e13168bcae41bc444e924da&username=NAME&password=PSW"
    4. Dim tempCookies As New CookieContainer
    5. Dim encoding As New UTF8Encoding
    6. Dim byteData As Byte() = encoding.GetBytes(postData)
    7. Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://instagram.com/accounts/login/next=/oauth/authorize/%3Fclient_id%3D9d836570317f4c18bca0db6d2ac38e29%26redirect_uri%3Dhttp%253A%252F%252Fweb.stagram.com%252F%26response_type%3Dcode%26scope%3Dlikes%2Bcomments%2Brelationships"), HttpWebRequest)
    8. postReq.Method = "POST"
    9. postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1"
    10. postReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
    11. postReq.KeepAlive = True postReq.Credentials = CredentialCache.DefaultCredentials
    12. postReq.CookieContainer = tempCookies postReq.ContentType = "application/x-www-form-urlencoded"
    13. postReq.Referer = "https://instagram.com/accounts/login/?next=/accounts/edit/"
    14. postReq.ContentLength = byteData.Length
    15. Dim postreqstream As Stream = postReq.GetRequestStream()
    16. postreqstream.Write(byteData, 0, byteData.Length)
    17. postreqstream.Close()
    18. Dim postresponse As HttpWebResponse
    19. postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
    20. tempCookies.Add(postresponse.Cookies)
    21. logincookie = tempCookies
    22. Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
    23. Dim thepage As String = postreqreader.ReadToEnd
    24. WebBrowser1.DocumentText = thepage
    25. End Sub



    In der Praxis funktioniert der Code bei eigtl. allen Webseiten und ich kann auch keinen Fehler erkennen. Nur bei Instagram scheint es Probleme zu geben, denn dort erscheint der Fehler "403" (= Forbitten). Kann mir jmd. helfen? Liegt es vlt. an "https"?
    Lad dir mal Tampa oder LiveHttpheaders fürn firefox runter damit du alles siehst.
    Mein Tampa sagt mir:
    Spoiler anzeigen
    23:35:22.781[453ms][total 572ms] Status: 200[OK]
    POST https://instagram.com/accounts/login/?next=/accounts/edit/ Load Flags[LOAD_DOCUMENT_URI LOAD_INITIAL_DOCUMENT_URI ] Größe des Inhalts[1487] Mime Type[text/html]
    Request Header:
    Host[instagram.com]
    User-Agent[Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1]
    Accept[text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8]
    Accept-Language[de-de,de;q=0.8,en-us;q=0.5,en;q=0.3]
    Accept-Encoding[gzip, deflate]
    DNT[1]
    Connection[keep-alive]
    Referer[instagram.com/accounts/login/?next=/accounts/edit/]
    Cookie[csrftoken=177dc40023df3971831be1981428e79e]
    POST-Daten:
    csrfmiddlewaretoken[177dc40023df3971831be1981428e79e]
    username[123]
    password[456]
    Response Header:
    Cache-Control[max-age=0]
    Content-Encoding[gzip]
    Content-Language[de]
    Content-Type[text/html]
    Date[Sun, 07 Oct 2012 21:35:24 GMT]
    Expires[Sun, 07 Oct 2012 21:35:24 GMT]
    Last-Modified[Sun, 07 Oct 2012 21:35:24 GMT]
    Server[nginx]
    Set-Cookie[csrftoken=177dc40023df3971831be1981428e79e; expires=Sun, 06-Oct-2013 21:35:24 GMT; Max-Age=31449600; Path=/]
    Vary[Cookie, Accept-Language, Accept-Encoding]
    X-Frame-Options[SAMEORIGIN]
    Content-Length[1487]
    Connection[keep-alive]


    Ergo sendest du 1. an die falsche Url und vergisst 2. das Cookie mitzusenden. Das Cookie bekommste, wenn du einmal nen get request auf die Url machst (ich hab den cookie wert ausm firefox rauskopiert, der hält aber auch net ewig).

    VB.NET-Quellcode

    1. Dim tempCookies As New CookieContainer
    2. Dim url As String = "https://instagram.com/"
    3. Dim requrl As String = "https://instagram.com/accounts/login/?next=/accounts/edit/"
    4. Dim cookie As String = "csrftoken=177dc40023df3971831be1981428e79e"
    5. tempCookies.SetCookies(New Uri(url), cookie)
    6. Dim username As String = "123"
    7. Dim password As String = "456"
    8. Dim postdata As String = String.Format("csrfmiddlewaretoken={0}&username={1}&password={2}", "177dc40023df3971831be1981428e79e", username, password)
    9. Dim byteData As Byte() = Encoding.UTF8.GetBytes(postdata)
    10. Dim postReq As HttpWebRequest = CType(WebRequest.Create(requrl), HttpWebRequest)
    11. postReq.Method = WebRequestMethods.Http.Post
    12. ...


    Voila und es funktioniert.
    Sorry, dass ich hier Poste, aber ich hab genau das selbe problem:

    @markus.obi

    Ich will auf der Seite "http://web.stagram.com/?t=lo" einloggen, wenn man auf login klickt, dann wird man an instagram weitergeleitet.

    VB.NET-Quellcode

    1. Imports System.Text
    2. Imports System.IO
    3. Imports System.Net
    4. Public Class Account
    5. Dim cookieCon As New CookieContainer
    6. Dim request As HttpWebRequest
    7. Dim response As HttpWebResponse
    8. #Region "Properties"
    9. Dim constUsername As String
    10. Public Property Username As String
    11. Get
    12. Username = constUsername
    13. End Get
    14. Set(value As String)
    15. constUsername = value
    16. End Set
    17. End Property
    18. Dim constPassword As String
    19. Public Property Password As String
    20. Get
    21. Password = constPassword
    22. End Get
    23. Set(value As String)
    24. constPassword = value
    25. End Set
    26. End Property
    27. #End Region
    28. #Region "GetResponse"
    29. Public Function GetResponse(ByVal url As String) As String
    30. 'request.Proxy = New WebProxy(New Uri(My.Settings.proxy))
    31. request = CType(HttpWebRequest.Create(url), HttpWebRequest)
    32. request.CookieContainer = cookieCon
    33. response = CType(request.GetResponse(), HttpWebResponse)
    34. Return New StreamReader(response.GetResponseStream()).ReadToEnd()
    35. End Function
    36. Public Function GetResponse(ByVal url As String, ByVal post As String) As String
    37. request = CType(HttpWebRequest.Create(url), HttpWebRequest)
    38. request.Method = "POST"
    39. request.CookieContainer = cookieCon
    40. request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0"
    41. request.ContentType = "application/x-www-form-urlencoded"
    42. Dim byteArr() As Byte = Encoding.Default.GetBytes(post)
    43. request.ContentLength = byteArr.Length
    44. Dim dataStream As Stream = request.GetRequestStream()
    45. dataStream.Write(byteArr, 0, byteArr.Length)
    46. response = CType(request.GetResponse(), HttpWebResponse)
    47. Return New StreamReader(response.GetResponseStream()).ReadToEnd()
    48. End Function
    49. #End Region
    50. Public Function Login(ByVal Account As Account) As Boolean
    51. Dim PostData As New StringBuilder
    52. Dim html As String = GetResponse("https://instagram.com/accounts/login/?next=/oauth/authorize/%3Fclient_id%3D9d836570317f4c18bca0db6d2ac38e29%26redirect_uri%3Dhttp%3A//web.stagram.com/%26response_type%3Dcode%26scope%3Dlikes%2Bcomments%2Brelationships#")
    53. Dim Token As String = GetToken(html)
    54. PostData.Append("csrfmiddlewaretoken=" & GetToken(html))
    55. PostData.Append("&username=" & Me.Username)
    56. PostData.Append("&password=" & Me.Password)
    57. MsgBox(Token)
    58. html = GetResponse("https://instagram.com/accounts/login/?next=/oauth/authorize/%3Fclient_id%3D9d836570317f4c18bca0db6d2ac38e29%26redirect_uri%3Dhttp%3A//web.stagram.com/%26response_type%3Dcode%26scope%3Dlikes%2Bcomments%2Brelationships", PostData.ToString())
    59. If html.Contains("Gib bitte einen korrekten Nutzernamen und ein Passwort ein") Then
    60. Return False
    61. Else
    62. Return True
    63. End If
    64. End Function
    65. Private Function GetToken(ByVal SourceCode As String) As String
    66. Return SourceCode.Substring(SourceCode.IndexOf("csrfmiddlewaretoken")).Split("""")(2).Split("""")(0)
    67. End Function
    68. End Class


    PS: habs auch damit versucht den Request auf diese seite zu jagen instagram.com/accounts/login/?next=/accounts/edit/

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „windowsfan“ ()