Httpwebrequest enable cookies

TehEpidemick

BANNED
Joined
Oct 31, 2011
Messages
778
Reaction score
183
I am trying to login to facebook using httpwebrequests. My problem is an error on facebook login stating
"Cookies RequiredCookies are not enabled on your browser. Please adjust this in your security preferences before continuing."

Code:
Imports System.NetImports System.Text
Imports System.IO


Public Class Form1
    Dim logincookie As CookieContainer


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim postdata As String = "lsd=AVrEI81J&email=&pass=&default_persistent=0&timezone=240&lgnrnd=180955_8SwZ&lgnjs=1375751401&locale=en_US"
        Dim tempcookies As New System.Net.CookieContainer
        Dim encoding As New UTF8Encoding
        Dim bytedata As Byte() = encoding.GetBytes(postdata)


        Dim postreq As HttpWebRequest = DirectCast(WebRequest.Create("https://www.facebook.com/login.php?login_attempt=1"), HttpWebRequest)
            postreq.CookieContainer = tempcookies
            postreq.Method = "POST"
            postreq.KeepAlive = True
            postreq.ContentType = "application/x-www.form-urlencoded"
            postreq.Referer = "https://www.facebook.com/login.php?login_attempt=1"
            postreq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"
            postreq.ContentLength = bytedata.Length
            Dim postreqstream As Stream = postreq.GetRequestStream()
            postreqstream.Write(bytedata, 0, bytedata.Length)
            postreqstream.Close()
            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


            RichTextBox1.Text = thepage


    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WebBrowser1.DocumentText = RichTextBox1.Text
    End Sub
End Class

If anyone has any idea it would be amazing.

Tehepidemick.
 
I made some slight adjustments to your code here. I did away with tempcookie. It's been a while, but I seem to remember something about having to set you request's cookie container to a new cookie container. When you are finished, you simply set logincookie to your request's cookie container. I can't test this right now. Let me know if it helps.

Code:
Imports System.NetImports System.Text
Imports System.IO


Public Class Form1
    Dim logincookie As CookieContainer


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim postdata As String = "lsd=AVrEI81J&email=&pass=&default_persistent=0&timezone=240&lgnrnd=180955_8SwZ&lgnjs=1375751401&locale=en_US"
        Dim encoding As New UTF8Encoding
        Dim bytedata As Byte() = encoding.GetBytes(postdata)


        Dim postreq As HttpWebRequest = DirectCast(WebRequest.Create("https://www.facebook.com/login.php?login_attempt=1"), HttpWebRequest)
            postreq.CookieContainer = New CookieContainer()
            postreq.Method = "POST"
            postreq.KeepAlive = True
            postreq.ContentType = "application/x-www.form-urlencoded"
            postreq.Referer = "https://www.facebook.com/login.php?login_attempt=1"
            postreq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"
            postreq.ContentLength = bytedata.Length
            Dim postreqstream As Stream = postreq.GetRequestStream()
            postreqstream.Write(bytedata, 0, bytedata.Length)
            postreqstream.Close()
            Dim postresponse As HttpWebResponse
            postresponse = DirectCast(postreq.GetResponse(), HttpWebResponse)
            tempcookies.Add(postresponse.Cookies)
            logincookie = postreq.CookieContainer()
            Dim postreqreader As New StreamReader(postresponse.GetResponseStream())


            Dim thepage As String = postreqreader.ReadToEnd


            RichTextBox1.Text = thepage


    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WebBrowser1.DocumentText = RichTextBox1.Text
    End Sub
End Class
 
A workaround is using the windows forms browser, it's one of most flexible ways of performing web requests. You'll be able to have cookies. Downside is offcourse the extra weight.
 
A workaround is using the windows forms browser, it's one of most flexible ways of performing web requests. You'll be able to have cookies. Downside is offcourse the extra weight.

Simply not worth it using browser for multithreaded apps, and webrequests are sexier :D
 
I went down this route before.
I'm guessing what you are programming isn't something that facebook's own API allows.
If it is, then by all means use it.

Answer :
Facebook is special and gives you cookies when you visit their site initially.
It then does some checks with those cookies when logging in.
So in order for the login to work on facebook (one of the few exceptions)
1.You must send a normal get request to the facebook homepage (httpwebrequest)
2.capture the cookies using a cookie container.
3.send a post request to login using the previously contained cookies.

If you do it like that, it will work. (I had the same thing happen a while back and looked everywhere)

Hope that helps! :D

^ yeah true that, POF does the same thing

I put this project on hold over this. Im very happy I came back to check. Thanks..and +rep...

and yeah, facebook probably frowns on what im doing
 
I went down this route before.
I'm guessing what you are programming isn't something that facebook's own API allows.
If it is, then by all means use it.

Answer :
Facebook is special and gives you cookies when you visit their site initially.
It then does some checks with those cookies when logging in.
So in order for the login to work on facebook (one of the few exceptions)
1.You must send a normal get request to the facebook homepage (httpwebrequest)
2.capture the cookies using a cookie container.
3.send a post request to login using the previously contained cookies.

If you do it like that, it will work. (I had the same thing happen a while back and looked everywhere)

Hope that helps! :D

Exactly. I do the same thing - login with any gibberish post data and then store the cookies into a cookiejar. Login the second time = success.
 
Hey Brother, this is pretty much the code I use here:

Code:
Imports System.Net
Imports System.Text
Imports System.IO

Public Class Form1

    Dim logincookie As CookieContainer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim postData As String = "referer=http%3A%2F%2Fforums.zybez.net%2Findex.php%3Fapp%3Dcore%26module%3Dglobal%26section%3Dlogin&username=" & TextBox1.Text & "&password=" & TextBox2.Text & "&rememberMe=1"
        Dim tempCookies As New CookieContainer
        Dim encoding As New UTF8Encoding
        Dim byteData As Byte() = encoding.GetBytes(postData)

        Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://forums.zybez.net/index.php?app=core&module=global&section=login&do=process"), HttpWebRequest)
        postReq.Method = "POST"
        postReq.KeepAlive = True
        postReq.CookieContainer = tempCookies
        postReq.ContentType = "application/x-www-form-urlencoded"
        postReq.Referer = "http://forums.zybez.net/index.php?app=core&module=global&section=login&do=process"
        postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
        postReq.ContentLength = byteData.Length

        Dim postreqstream As Stream = postReq.GetRequestStream()
        postreqstream.Write(byteData, 0, byteData.Length)
        postreqstream.Close()
        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

        RichTextBox1.Text = thepage

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WebBrowser1.DocumentText = RichTextBox1.Text
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        WebBrowser1.DocumentText = RichTextBox2.Text
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim request As HttpWebRequest = DirectCast(WebRequest.Create("http://forums.zybez.net/index.php?app=core&module=usercp"), HttpWebRequest)
        request.CookieContainer = logincookie
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Dim reader As New StreamReader(response.GetResponseStream())
        Dim theusercp As String = reader.ReadToEnd

        RichTextBox2.Text = theusercp

    End Sub
End Class

Just change the values such as referer and such and you are golden.
Hope this helps you.
 
more easy to use access token and then use the graph api to send every request. With graph api the only thing that you cant do is like a fan page... over this you can do every thing
 
more easy to use access token and then use the graph api to send every request. With graph api the only thing that you cant do is like a fan page... over this you can do every thing

the token would work, but im doing alot of scraping liking and friend requesting so...
 
Back
Top