FREE VB.NET SOURCE CODE

27 March, 2012

[SNP] Using HTTPWebRequests to login to Facebook [VB.NET]

Dim cookieJar As New Net.CookieContainer()
  Dim request As Net.HttpWebRequest
  Dim response As Net.HttpWebResponse
  Dim strURL As String = ""

  Try
    'Get Cookies
    strURL = "https://login.facebook.com/login.php"
    request = CType(HttpWebRequest.Create(strURL), HttpWebRequest)
    request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
    request.Method = "GET"
    request.CookieContainer = cookieJar
    response = CType(request.GetResponse(), HttpWebResponse)

    For Each tempCookie As Net.Cookie In response.Cookies
    cookieJar.Add(tempCookie)
    Next

    'Send the post data now
    request = CType(HttpWebRequest.Create(strURL), HttpWebRequest)
    request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
    request.Method = "POST"
    request.AllowAutoRedirect = True
    request.CookieContainer = cookieJar

    Dim writer As StreamWriter = New StreamWriter(request.GetRequestStream())
    writer.Write("email=username&pass=password")
    writer.Close()
    response = CType(request.GetResponse(), HttpWebResponse)

    'Get the data from the page
    Dim stream As StreamReader = New StreamReader(response.GetResponseStream())
    Dim data As String = stream.ReadToEnd()
    response.Close()

    If data.Contains("<title>Facebook") = True Then
    'LOGGED IN SUCCESSFULLY
    MessageBox.Show("logged in sucessfully")
    Else
    MessageBox.Show("Not logged in")
    End If

  Catch ex As Exception
    MessageBox.Show(ex.tostring)
  End Try

With this basic HTTPWebRequest you can check if the login details - user and password are correct .

1 comment:

  1. Kudos man that solved my problem, {btw If data.Contains(-title-Facebook) = True} returns always false...

    ReplyDelete