PutterPlace
Active member
- Joined
- Feb 18, 2008
- Messages
- 37
- Programming Experience
- 1-3
I'm attempting to use the HttpWebRequest class to log in to Gmail, but I'm having some problems. Gmail doesn't seem to accept the login data. It returns to the login form, and an error is shown as if a username wasn't supplied. I have checked that the correct form variables are used for logging in, but I can't seem to find the problem. Here is my code as of right now:
Any help getting this to work would be greatly appreciated.
VB.NET:
Dim CookieJar As CookieContainer = New CookieContainer()
Dim Stream3 As Stream
Dim Temp3 As String = Nothing
Dim Request3 As HttpWebRequest = Nothing
Dim Response3 As HttpWebResponse = Nothing
Try
Request3 = HttpWebRequest.Create("http://www.gmail.com")
If Form2.lbProxies.Items.Count > 0 Then
Dim ProxyArray As Array = Split(Form2.lbProxies.Items.Item(Form2.lbProxies.SelectedIndex), ":")
Dim ProxyServer As String = ProxyArray(0)
Dim ProxyPort As Integer = ProxyArray(1).ToString
Request3.Proxy = New WebProxy("http://" & ProxyServer & ":" & ProxyPort)
End If
Request3.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"
Request3.CookieContainer = CookieJar
Response3 = Request3.GetResponse()
Stream3 = Response3.GetResponseStream
Temp3 = New StreamReader(Stream3).ReadToEnd()
Stream3.Close()
Response3.Close()
Catch ex As Exception
End Try
Dim postData As String
Dim responseData As String
Dim SW As StreamWriter = Nothing
Dim SR As StreamReader = Nothing
Dim Request1 As HttpWebRequest = Nothing
Dim Response1 As HttpWebResponse = Nothing
postData = "ltmpl=default"
postData &= "<mplcache=2"
postData &= "&continue=" & UrlEncode("http://mail.google.com/mail/?ui=html&zy=l")
postData &= "&service=mail"
postData &= "&rm=false"
postData &= "<mpl=default"
postData &= "&hl=en"
postData &= "<mpl=default"
postData &= "&Email=" & UrlEncode("USERNAME HERE")
postData &= "&Passwd=" & UrlEncode("PASSWORD HERE")
postData &= "&PersistentCookie=yes"
postData &= "&rmshown=1"
postData &= "&signIn=" & UrlEncode("Sign in")
postData &= "&asts="
Request1 = WebRequest.Create("https://www.google.com/accounts/ServiceLoginAuth?service=mail")
Request1.Method = "POST"
Request1.CookieContainer = CookieJar
Request1.ContentType = "application/x-www-form-urlencoded"
Request1.ContentLength = postData.Length
Request1.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"
Try
SW = New StreamWriter(Request1.GetRequestStream())
SW.Write(postData)
Catch ex As Exception
Throw ex
Finally
SW.Close()
End Try
Try
Response1 = Request1.GetResponse()
SR = New StreamReader(Response1.GetResponseStream())
responseData = SR.ReadToEnd()
Catch Wex As System.Net.WebException
SR = New StreamReader(Wex.Response.GetResponseStream())
responseData = SR.ReadToEnd()
Throw New Exception(responseData)
MsgBox(responseData)
Finally
SR.Close()
Response1.Close()
End Try
Dim goToURL As String = responseData.Substring(responseData.IndexOf("'") + 5)
goToURL = goToURL.Substring(0, goToURL.IndexOf("'"))
Dim Stream2 As Stream
Dim Temp2 As String
Dim Request2 As HttpWebRequest = Nothing
Dim Response2 As HttpWebResponse = Nothing
Try
Request2 = HttpWebRequest.Create(goToURL)
Request2.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"
Request2.CookieContainer = CookieJar
Response2 = Request2.GetResponse()
Stream2 = Response2.GetResponseStream
Temp2 = New StreamReader(Stream2).ReadToEnd()
Stream2.Close()
Response2.Close()
Catch ex As Exception
End Try
Any help getting this to work would be greatly appreciated.