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

Login to Google with Webrequests

Discussion in 'Visual Basic .NET' started by nodyguy, Feb 2, 2014.

  1. nodyguy

    nodyguy Jr. VIP Jr. VIP

    Joined:
    Feb 8, 2012
    Messages:
    369
    Likes Received:
    60
    Gender:
    Male
    Hi guys Im learning webrequests and I have a problem logging into google account.
    First I click "Make Request" and then I click see code to see how the html would look like in a webbrowser and it says I have cookies disabled..
    My App:
    [​IMG]


    Here is my code :
    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 = "GALX=wWuDJOaiIOg&continue=https%3A%2F%2Fwww.google.co.il%2F%3Fgws_rd%3Dcr%26ei%3Dw1XtUpPOHeLo4gTuq4GwCQ%26authuser%3D0&hl=en&_utf8=%E2%98%83&bgresponse=%21A0J7g8jYeZLzYETHWkGePyCNzQIAAAAXUgAAAAQqAOerAuj6CJFAwz1LOfLPLnp_tShLUn0R8h05q813-0vYwSD-aSMroEgro6AQkLxHnyjbwCO8p86e1gei1w4CNJCTGwTxhcIf9hCVb9Gbwfce-WuzkfXpA5SOVdKayNMoLvXzwBvBK8kqskQW4tpRDZKlbyHlmjI5CKIQapG3Vy-BTcOm66IxQUQy6hnF131k-KtKLYPSQSqM6OG5_KzbtmJJUXoCP1wa9XMOfAisQoFb88I5U7QqkmPIOdLFVnf1NgHiSWinvHgMVCxPC0-xWnQd-CfJ5Q-OWYKFcMZI5OfUMcqu0-QHIFM&pstMsg=1&dnConn=&checkConnection=youtube%3A165%3A1&checkedDomains=youtube&Email=" & TextBox1.Text & "&Passwd=" & TextBox2.Text & "&signIn=Sign+in&rmShown=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://accounts.google.com/ServiceLoginAuth"), HttpWebRequest)
            postReq.Method = "POST"
            postReq.KeepAlive = True
            postReq.CookieContainer = tempCookies
            postReq.ContentType = "application/x-www-form-urlencoded"
            postReq.Referer = "http://accounts.google.com/ServiceLoginAuth"
    
    
    
    
            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
    
    
    End Class
     
  2. Shirko

    Shirko Junior Member

    Joined:
    Aug 11, 2012
    Messages:
    191
    Likes Received:
    170
    Location:
    adding monkeys to my papal
    You won't be able to log-in to google using HttpWebRequests... it's a high-level programming class that it is very limited and structured. You won't go far using it... trust me.

    You'd better go for a better low-level solution and implement your own HTTP protocol under a TCP/IP connection. If you're using .NET Framework, you have the TCPClient class. You'll have to handle requests, responses, cookies, headers, everything as a Web Browser would do and try to look as legit as possible, because for sure, google has ways of detecting web scrapers/ web automation.

    Basically, you'll have to emulate every single packet that goes out of your Web browser. If you use Firefox, you can sniff them using Live HTTP headers plugin which is quite simple.

    This is the way real bots are coded.

    Good luck.
     
  3. gtothem

    gtothem Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 22, 2011
    Messages:
    236
    Likes Received:
    296
    Occupation:
    Coder
    Home Page:
    Ignore the above post. HttpWebRequests is fully capable of doing what you want.

    You need to code some cookie handling, which can be a little tricky if you are new to VB and webrequests.

    Here is a little idea of how its done:

    Firstly send a GET request to the google page. You do this so google can set initial cookies.
    Code:
                [B]Dim cookies As CookieCollection[/B]
                Dim request As HttpWebRequest = HttpWebRequest.Create("#url")
                Dim encoding As New UTF8Encoding
                request.CookieContainer = New CookieContainer
                Dim dataStream As Stream
                Dim response As HttpWebResponse = request.GetResponse()
                [B]cookies = response.Cookies[/B]
                dataStream = response.GetResponseStream()
                Dim reader As New StreamReader(dataStream)
                Dim responseFromServer As String = reader.ReadToEnd()
    
    Then you make the POST with the cookie collection

    Code:
                Dim encoding As New UTF8Encoding
                Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("#url"), HttpWebRequest)
                Dim newpostData As String = "blablalba"
                Dim byteData As Byte() = encoding.GetBytes(newpostData)
                postReq.Method = "POST"
                postReq.KeepAlive = True
                postReq.ContentType = "application/x-www-form-urlencoded"
                postReq.AllowAutoRedirect = True
                postReq.Referer = "#url"
                postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0"
                postReq.ContentLength = byteData.Length
                [B]postReq.CookieContainer = New CookieContainer
                postReq.CookieContainer.Add(cookies)[/B]
    
    If you run into trouble just google webrequest cookie handling.. it's all out there.
     
    • Thanks Thanks x 1
  4. zohar

    zohar Newbie

    Joined:
    Jun 24, 2014
    Messages:
    44
    Likes Received:
    5
    Dude, Google is one big AJAX hell.. you will not succeed with TCP or HTTP level apps. You need a fully enabled Javascript application, hence, using IE or the WebBrowser control is a absolute minimum requirement to get the job done..

    Good luck on yer project.
     
  5. GreedHF

    GreedHF Newbie

    Joined:
    Nov 14, 2014
    Messages:
    15
    Likes Received:
    2
    You have no clue what you're talking about, do you?
     
    • Thanks Thanks x 1
  6. zohar

    zohar Newbie

    Joined:
    Jun 24, 2014
    Messages:
    44
    Likes Received:
    5
    Probably not, but my g-mail bot works very nicely anyway!
     
  7. hajro

    hajro Junior Member

    Joined:
    Oct 4, 2014
    Messages:
    164
    Likes Received:
    43
    Try with GET request and save cookies from that request, then use that cookies in next POST request.
     
  8. zohar

    zohar Newbie

    Joined:
    Jun 24, 2014
    Messages:
    44
    Likes Received:
    5
    But hey dude, if you think I am rambling out of my ass, fire up Chrome, login to your Gmail

    Now start the Javascript console, click on the Network tab, and move your mouse over some document elements. You will see what I am talking about, Screwgle is continously in touch with gmail server to track each and every action you do.
     
    Last edited: Nov 14, 2014
  9. mktanny

    mktanny Regular Member

    Joined:
    Oct 22, 2009
    Messages:
    225
    Likes Received:
    62
    Occupation:
    Blog editor and IM



    What if someone uses keyboard to navigate through chrome ?
     
    Last edited: Nov 14, 2014