Facebook Http WebRequest/Response Cookie Problem

  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von roteisen^.

    Facebook Http WebRequest/Response Cookie Problem

    Hallo liebe VB Community,
    dies ist mein erster Beitrag und ich komme gleich mit einer Frage;)
    Ich arbeite im moment an einem Facebook - Freunde Ad Programm mit VB NET 2010. Wollte meinen Bot nicht auf Basis eines Form mit WebBrowser Tool sondern als kompakte Konsolenanwendung entwickeln mit HttpWeb Response/Request .

    Login - funktioniert soweit
    Jedoch hab ich irgendwas bei den Cookies falsch gemacht...
    Profil besuchen - und wir sind nicht eingeloggt, kommmen zwar aufs Profil aber ohne eingeloggt zu sein bzw wurden die Cookies nicht erkannt.

    VB.NET-Quellcode

    1. Imports System.Net
    2. Imports System.Text.RegularExpressions
    3. Imports System.Text
    4. Imports System.IO
    5. Module Module1
    6. Dim logincookie As CookieContainer
    7. Dim PEmail As String
    8. Dim PPass As String
    9. Public Structure FBLoginData
    10. Public EMail As String
    11. Public Password As String
    12. Public lsd As String
    13. Public charset As String
    14. End Structure
    15. Private Function GetLoginData(ByVal EMail As String, ByVal Password As String, ByVal SourceCode As String) As FBLoginData
    16. Dim FBData As New FBLoginData()
    17. FBData.EMail = PEmail
    18. FBData.Password = PPass
    19. Dim lsdPattern As String = "<input type=""hidden"" name=""lsd"" value=""(?<lsd>.+?)"" />"
    20. Dim lsd As Match = Regex.Match(SourceCode, lsdPattern)
    21. FBData.lsd = lsd.Groups("lsd").Value
    22. Dim charsetPattern As String = "<input type=""hidden"" name=""charset_test"" value=""(?<charset>.+?)"" />"
    23. Dim charset As Match = Regex.Match(SourceCode, charsetPattern)
    24. FBData.charset = charset.Groups("charset").Value
    25. Return FBData
    26. End Function
    27. Sub Main()
    28. ' Willkommensnachricht
    29. Console.WriteLine(" *******************************\n")
    30. Console.WriteLine(" * Facebook Bot by Roteisen*\n")
    31. Console.WriteLine(" *******************************\n")
    32. ' Public Var PEmail und PPass werden deklariert.
    33. Console.WriteLine()
    34. Console.WriteLine()
    35. Console.WriteLine(" - Bitte Facebook E-Mail eingeben: -")
    36. Console.WriteLine()
    37. PEmail = Console.ReadLine()
    38. Console.WriteLine()
    39. Console.WriteLine(" - Bitte Facebook Passwort eingeben: -")
    40. Console.WriteLine()
    41. PPass = Console.ReadLine()
    42. Console.WriteLine()
    43. Console.WriteLine()
    44. Console.WriteLine(" processing Login ...")
    45. Threading.Thread.Sleep(2000)
    46. intlogin()
    47. Console.WriteLine()
    48. Console.WriteLine(" - Login Succes! -")
    49. Console.WriteLine()
    50. Console.WriteLine("-------------------------------------------------------")
    51. Console.WriteLine()
    52. FBProfil()
    53. Console.WriteLine("Profil besuchen")
    54. Console.ReadLine()
    55. End Sub
    56. Sub intlogin()
    57. 'Daten varuser & varpass werden mit postData versendet !
    58. '/Dim postData As String = "charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&lsd=8WiEa&locale=de_DE&email=" & PEmail & "&pass=" & PPass & "&default_persistent=0&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&lsd=8WiEa"
    59. '/Dim tempCookies As New CookieContainer
    60. 'Enconding, da Http keinen Stream warnehmen kann
    61. '/Dim encoding As New UTF8Encoding
    62. '/Dim byteData As Byte() = encoding.GetBytes(postData)
    63. 'Login zu Facebook
    64. '/ Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://www.facebook.com/login.php?login_attempt=1"), HttpWebRequest)
    65. '/ postReq.Method = "POST"
    66. '/postReq.KeepAlive = True
    67. '/ postReq.CookieContainer = tempCookies
    68. '/ postReq.ContentType = "pplication/x-www-form-urlencoded"
    69. '/postReq.Referer = "https://www.facebook.com/login.php?login_attempt=1"
    70. '/postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0a2) Gecko/20110613 Firefox/6.0a2"
    71. '/postReq.ContentLength = byteData.Length
    72. '/ Dim postreqstream As Stream = postReq.GetRequestStream()
    73. '/postreqstream.Write(byteData, 0, byteData.Length)
    74. '/postreqstream.Close()
    75. '/ Dim postresponse As HttpWebResponse
    76. '/ postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
    77. '/tempCookies.Add(postresponse.Cookies)
    78. '/ logincookie = tempCookies
    79. '/ Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
    80. '/ My.Computer.FileSystem.WriteAllText("C:\Debug\x5.html", postreqreader.ReadToEnd(), False)
    81. '/ postreqreader.Close()
    82. Dim uri As New Uri("http://www.facebook.com/login.php?login_attempt=1")
    83. Dim cookieContainer As New CookieContainer()
    84. Dim FBData As FBLoginData
    85. Dim tempCookies As New CookieContainer
    86. ' Infos auslesen (Wegen Cookies)
    87. 'Dim sessionRequest As HttpWebRequest = DirectCast(WebRequest.Create(uri), HttpWebRequest)
    88. 'sessionRequest.CookieContainer = New CookieContainer()
    89. 'logincookie = sessionRequest.CookieContainer
    90. 'sessionRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"
    91. 'Dim sessionResponse As HttpWebResponse = DirectCast(sessionRequest.GetResponse(), HttpWebResponse)
    92. 'Dim checkReader As New StreamReader(sessionResponse.GetResponseStream())
    93. 'FBData = GetLoginData(FBData.EMail, FBData.Password, checkReader.ReadToEnd())
    94. 'checkReader.Close()
    95. 'sessionResponse.Close()
    96. ' Einloggen
    97. Dim loginData As String = "charset_test=" & FBData.charset & "&lsd=" & FBData.lsd & "&locale=de_DE&email=" & FBData.EMail & "&pass=" & FBData.Password & "&persistent=1&default_persistent=1&charset_test=" & FBData.charset & "&lsd=" & FBData.lsd
    98. Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("http://www.facebook.com/login.php?login_attempt=1"), HttpWebRequest)
    99. req.CookieContainer = logincookie
    100. req.Method = "POST"
    101. req.ContentType = "application/x-www-form-urlencoded"
    102. req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"
    103. Dim encoding As New ASCIIEncoding()
    104. Dim loginDataBytes As Byte() = encoding.GetBytes(loginData)
    105. req.ContentLength = loginDataBytes.Length
    106. Dim stream As IO.Stream = req.GetRequestStream()
    107. stream.Write(loginDataBytes, 0, loginDataBytes.Length)
    108. stream.Close()
    109. Dim res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
    110. tempCookies.Add(res.Cookies)
    111. logincookie = tempCookies
    112. Return
    113. End Sub
    114. Sub FBProfil()
    115. Dim tempCookies As New CookieContainer
    116. ' Test ID 100003177993836 : , aus Txt Holen IDs
    117. Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://www.facebook.com/profile.php?id=100003177993836"), HttpWebRequest)
    118. request.CookieContainer = logincookie
    119. request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"
    120. Dim res As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
    121. tempCookies.Add(res.Cookies)
    122. logincookie = tempCookies
    123. Dim str As New StreamReader(res.GetResponseStream)
    124. My.Computer.FileSystem.WriteAllText("C:\Debug\x53.html", str.ReadToEnd(), False)
    125. res.Close()
    126. Return
    127. End Sub
    128. End Module


    Hoffe jemand kann mir weiterhelfen, denn ich find den Fehler irgendwie nicht.

    roteisen^ schrieb:

    Login - funktioniert soweit

    Nun, wieso nimmst Du an, daß der Login funktioniert hat ? Mir sieht es eher danach aus als ob Du genau daran scheiterst, da Du sofort mit einem POST Request beginnst

    Versuchs mal in folgender Reihenfolge:
    - einen leeren Cookiecontainer anlegen
    - mit einem GET Request auf die Login Seite gehen (-> Cookiecontainer wird automatisch mit Sessioncookie gefüllt)
    - Login Daten ermitteln
    - mit einem POST Request und dem gleichen CookieContainer den Login durchführen

    Kangaroo schrieb:

    roteisen^ schrieb:

    Login - funktioniert soweit

    Nun, wieso nimmst Du an, daß der Login funktioniert hat ? Mir sieht es eher danach aus als ob Du genau daran scheiterst, da Du sofort mit einem POST Request beginnst

    Versuchs mal in folgender Reihenfolge:
    - einen leeren Cookiecontainer anlegen
    - mit einem GET Request auf die Login Seite gehen (-> Cookiecontainer wird automatisch mit Sessioncookie gefüllt)
    - Login Daten ermitteln
    - mit einem POST Request und dem gleichen CookieContainer den Login durchführen
    Hallo,
    erstmal danke;) Habs gemacht und meinen alten Code überarbeitet

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Net
    2. Imports System.Text.RegularExpressions
    3. Imports System.Text
    4. Imports System.IO
    5. Module Module1
    6. Dim cookies As CookieContainer
    7. Dim PEmail As String
    8. Dim PPass As String
    9. Public Structure FBLoginData
    10. Public EMail As String
    11. Public Password As String
    12. Public lsd As String
    13. Public charset As String
    14. End Structure
    15. Private Function GetLoginData(ByVal EMail As String, ByVal Password As String, ByVal SourceCode As String) As FBLoginData
    16. Dim FBData As New FBLoginData()
    17. FBData.EMail = PEmail
    18. FBData.Password = PPass
    19. Dim lsdPattern As String = "<input type=""hidden"" name=""lsd"" value=""(?<lsd>.+?)"" />"
    20. Dim lsd As Match = Regex.Match(SourceCode, lsdPattern)
    21. FBData.lsd = lsd.Groups("lsd").Value
    22. Dim charsetPattern As String = "<input type=""hidden"" name=""charset_test"" value=""(?<charset>.+?)"" />"
    23. Dim charset As Match = Regex.Match(SourceCode, charsetPattern)
    24. FBData.charset = charset.Groups("charset").Value
    25. Return FBData
    26. End Function
    27. Sub Main()
    28. ' Willkommensnachricht
    29. Console.WriteLine(" *******************************\n")
    30. Console.WriteLine(" * Facebook Bot by Roteisen *\n")
    31. Console.WriteLine(" *******************************\n")
    32. ' Public Var PEmail und PPass werden deklariert.
    33. Console.WriteLine()
    34. Console.WriteLine()
    35. Console.WriteLine(" - Bitte Facebook E-Mail eingeben: -")
    36. Console.WriteLine()
    37. PEmail = Console.ReadLine()
    38. Console.WriteLine()
    39. Console.WriteLine(" - Bitte Facebook Passwort eingeben: -")
    40. Console.WriteLine()
    41. PPass = Console.ReadLine()
    42. Console.WriteLine()
    43. Console.WriteLine()
    44. Console.WriteLine(" processing Login ...")
    45. Threading.Thread.Sleep(2000)
    46. intlogin()
    47. Console.WriteLine()
    48. Console.WriteLine(" - Login Succes! -")
    49. Console.WriteLine()
    50. Console.WriteLine("-------------------------------------------------------")
    51. Console.WriteLine()
    52. FBProfil()
    53. Console.WriteLine("Profil besuchen")
    54. Console.ReadLine()
    55. End Sub
    56. Sub intlogin()
    57. Dim uri As New Uri("http://www.facebook.com/login.php?login_attempt=1")
    58. Dim cookieContainer As New CookieContainer()
    59. Dim FBData As FBLoginData
    60. ' Infos auslesen (Wegen Cookies)
    61. Dim sessionRequest As HttpWebRequest = DirectCast(WebRequest.Create(uri), HttpWebRequest)
    62. sessionRequest.CookieContainer = New CookieContainer()
    63. cookies = sessionRequest.CookieContainer
    64. sessionRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"
    65. Dim sessionResponse As HttpWebResponse = DirectCast(sessionRequest.GetResponse(), HttpWebResponse)
    66. Dim checkReader As New StreamReader(sessionResponse.GetResponseStream())
    67. FBData = GetLoginData(PEmail, PPass, checkReader.ReadToEnd())
    68. checkReader.Close()
    69. sessionResponse.Close()
    70. ' Einloggen
    71. Dim loginData As String = "charset_test=" & FBData.charset & "&lsd=" & FBData.lsd & "&locale=de_DE&email=" & FBData.EMail & "&pass=" & FBData.Password & "&persistent=1&default_persistent=1&charset_test=" & FBData.charset & "&lsd=" & FBData.lsd
    72. Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("http://www.facebook.com/login.php?login_attempt=1"), HttpWebRequest)
    73. req.CookieContainer = cookies
    74. req.Method = "POST"
    75. req.ContentType = "application/x-www-form-urlencoded"
    76. req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"
    77. Dim encoding As New ASCIIEncoding()
    78. Dim loginDataBytes As Byte() = encoding.GetBytes(loginData)
    79. req.ContentLength = loginDataBytes.Length
    80. Dim stream As IO.Stream = req.GetRequestStream()
    81. stream.Write(loginDataBytes, 0, loginDataBytes.Length)
    82. stream.Close()
    83. Dim res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
    84. Dim str As New StreamReader(res.GetResponseStream)
    85. My.Computer.FileSystem.WriteAllText("C:\debug\debugfb.html", str.ReadToEnd(), False)
    86. res.Close()
    87. End Sub
    88. Sub loginto()
    89. Dim logcookie As New CookieContainer
    90. Dim FBData As FBLoginData
    91. Dim fburl As String = "http://www.facebook.com/login.php?login_attempt=1"
    92. Dim request As HttpWebRequest = DirectCast(WebRequest.Create(fburl), HttpWebRequest)
    93. request.Method = "GET"
    94. logcookie = request.CookieContainer
    95. request.ContentType = "application/x-www-form-urlencoded"
    96. Dim sessionResponse As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
    97. Dim checkReader As New StreamReader(sessionResponse.GetResponseStream())
    98. FBData = GetLoginData(FBData.EMail, FBData.Password, checkReader.ReadToEnd())
    99. checkReader.Close()
    100. Dim loginData As String = "charset_test=" & FBData.charset & "&lsd=" & FBData.lsd & "&locale=de_DE&email=" & FBData.EMail & "&pass=" & FBData.Password & "&persistent=1&default_persistent=1&charset_test=" & FBData.charset & "&lsd=" & FBData.lsd
    101. Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("http://www.facebook.com/login.php?login_attempt=1"), HttpWebRequest)
    102. req.CookieContainer = cookies
    103. req.Method = "POST"
    104. req.ContentType = "application/x-www-form-urlencoded"
    105. req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"
    106. Dim encoding As New ASCIIEncoding()
    107. Dim loginDataBytes As Byte() = encoding.GetBytes(loginData)
    108. req.ContentLength = loginDataBytes.Length
    109. Dim stream As IO.Stream = req.GetRequestStream()
    110. stream.Write(loginDataBytes, 0, loginDataBytes.Length)
    111. stream.Close()
    112. End Sub
    113. Sub FBProfil()
    114. Dim profilcoock As New CookieContainer
    115. Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://www.facebook.com/profile.php?id=100003177993836"), HttpWebRequest)
    116. request.CookieContainer = cookies
    117. request.Method = "POST"
    118. request.ContentType = "application/x-www-form-urlencoded"
    119. request.UserAgent = "Mozilla/5.0 (Windo"
    120. '
    121. Dim res As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
    122. cookies.Add(res.Cookies)
    123. Dim str As New StreamReader(res.GetResponseStream)
    124. My.Computer.FileSystem.WriteAllText("C:\debug\debugfbprof.html", str.ReadToEnd(), False)
    125. res.Close()
    126. Return
    127. End Sub
    128. End Module


    Jetzt funktioniert der Login, und die Cookies werden gespeichert,
    Hab Testweise mal die zweite Funktion FBProfil() gestartet...
    Nun bin ich zwar eingeloggt aber es kommt der Fehler von Facebook "Leider konnte deine Anfrage nicht bearbeitet werden." ....
    was ist nun schief gelaufen.