Login to Google with Webrequests

nodyguy

Supreme Member
Joined
Feb 8, 2012
Messages
1,344
Reaction score
544
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:
6Grxn.png



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
 
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.
 
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.
 
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.
 
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.
You have no clue what you're talking about, do you?
 
Probably not, but my g-mail bot works very nicely anyway!
 
Try with GET request and save cookies from that request, then use that cookies in next POST request.
 
You have no clue what you're talking about, do you?

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:
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.




What if someone uses keyboard to navigate through chrome ?
 
Last edited:
Back
Top