1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Httpwebrequest enable cookies

Discussion in 'Visual Basic .NET' started by TehEpidemick, Aug 6, 2013.

  1. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    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.
     
  2. Psychop1

    Psychop1 Registered Member

    Joined:
    Jul 30, 2010
    Messages:
    63
    Likes Received:
    19
    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
     
  3. neutralhatter

    neutralhatter Jr. VIP Jr. VIP Premium Member

    Joined:
    Jun 23, 2010
    Messages:
    430
    Likes Received:
    330
    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.
     
  4. DarkPixel

    DarkPixel Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 4, 2011
    Messages:
    1,328
    Likes Received:
    1,239
    Location:
    ↓↓↓↓
    Home Page:
    Simply not worth it using browser for multithreaded apps, and webrequests are sexier :D
     
  5. r000k

    r000k Registered Member

    Joined:
    Jan 10, 2013
    Messages:
    66
    Likes Received:
    30
    ^ yeah true that, POF does the same thing
     
    • Thanks Thanks x 2
  6. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    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
     
  7. FJX

    FJX Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 13, 2011
    Messages:
    356
    Likes Received:
    186
    Location:
    0x90
    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.
     
  8. stevie

    stevie Newbie

    Joined:
    Jul 31, 2010
    Messages:
    48
    Likes Received:
    10
    Location:
    UK
    Home Page:
    You could always use the HTMLAgilityPack.

     
  9. roach

    roach BANNED BANNED

    Joined:
    Sep 8, 2009
    Messages:
    740
    Likes Received:
    395
    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.
     
    • Thanks Thanks x 1
  10. zx123

    zx123 Senior Member

    Joined:
    Feb 26, 2009
    Messages:
    1,158
    Likes Received:
    875
    Home Page:
    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
     
    • Thanks Thanks x 1
  11. TehEpidemick

    TehEpidemick BANNED BANNED

    Joined:
    Oct 31, 2011
    Messages:
    778
    Likes Received:
    181
    the token would work, but im doing alot of scraping liking and friend requesting so...
     
    • Thanks Thanks x 1