Enable cookies in WinForms webbrowser - vb.net

i was trying to make a bot for a facebook game .. the problem is that the user need to login to facebook . i tried to make this with httpwebrequest . everything went fine but i got an error saying my web browser doesn't support cookies
here is the code i used for the httpwebrequest
Dim postData As String = "lsd=AVrFBNXT&display=&enable_profile_selector=&legacy_return=1&next=&profile_selector_ids=&trynum=1&timezone=-120&lgnrnd=163248_FehM&lgnjs=1372203160&email=" & (TextBox1.Text) & "&pass=" & (TextBox2.Text) & "f&default_persistent=1"
Dim tempcookies As New CookieContainer
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postreq As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://www.facebook.com/login.php"), HttpWebRequest)
postreq.Method = "POST"
postreq.KeepAlive = True
postreq.CookieContainer = tempcookies
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.ContentType = "application/x-www-form-urlencoded"
postreq.Referer = "https://www.facebook.com/login.php"
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
WebBrowser1.DocumentText = thepage

Webbrowser control is essentially an embedded Internet Explorer and thus shares Internet Explorer's settings.
If your IE has cookies enabled then your WebBrowser control has them enabled too and your problem could be elsewhere.
To check your IE settings follow these steps:
Choose "Internet Options" under the "Tools" menu on the IE;
Select the "Privacy" tab.
Click the "Advanced..." button in the "Settings" groupbox.
Check the "Override automatic cookie handling" option.
Check both "Accept" options.
Click "OK"

Related

VB.Net httpwebrequest login and download page

I want to login with my account to VyprVPN website.
So i logged in successfully with my code below, but i want to get number of remaining data, which is stored in this url: https://www.goldenfrog.com/controlpanel/vpn-remaining as pure html.
So basically what i'm trying to do is to download html from url after login.
My login code:
Dim username As String = "myusername"
Dim password As String = "mypassword"
Dim postData As String = "username=" & username & "&password=" & password & "&login=Login"
Dim tempCookies As New CookieContainer
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://www.goldenfrog.com/controlpanel/login"), HttpWebRequest) 'prva linija
postReq.Method = "POST"
postReq.KeepAlive = True
postReq.Timeout = 15000
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "https://www.goldenfrog.com/login"
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"
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
What i tried (doesn't work, shows blank message box which is result of visiting that url without logging in, i probably failed to assign right cookie):
Dim request As HttpWebRequest = DirectCast(WebRequest.Create("https://www.goldenfrog.com/controlpanel/vpn-remaining"), HttpWebRequest)
request.CookieContainer = tempCookies
request.Method = "POST"
request.KeepAlive = True
request.CookieContainer = logincookie
request.ContentType = "application/x-www-form-urlencoded"
request.Referer = "https://www.goldenfrog.com/controlpanel"
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"
request.ContentLength = byteData.Length
Dim postreqstream2 As Stream = request.GetRequestStream()
postreqstream2.Write(byteData, 0, byteData.Length)
postreqstream2.Close()
Dim postresponse2 As HttpWebResponse
postresponse2 = DirectCast(request.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse2.Cookies)
logincookie = tempCookies
Dim postreqreader2 As New StreamReader(postresponse.GetResponseStream())
Dim thepage2 As String = postreqreader.ReadToEnd
MsgBox(thepage2)
Dim request As HttpWebRequest = DirectCast(WebRequest.Create("url"), 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

VB.net - HttpWebRequest POST (login to Instagram.com)

I would like to know how will see log into sites like instagram.com, as I have the same question for twitter, my doubt comes when trying to log in by code without the webbrowser using method post in vb.net, trying to use this code returns me on instagram Error 404:
Dim postData As String = "csrfmiddlewaretoken=" & TextBox1.Text & "&username=xxxxx&password=xxxxx"
Dim tempCookies As New CookieContainer
Dim encoding As New ASCIIEncoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://instagram.com/accounts/login/"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = True
postReq.CookieContainer = cokkie
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "https://instagram.com/accounts/login/"
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0"
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)
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
RichTextBox1.Text = thepage
I get the csrfmiddlewaretoken the instagram page by method get, really do not understand, but I think I read that I must first get a cookie before log in, but really do not know, if anyone can help me I would greatly appreciate
Are you looking for
http://instagram.com/developer/authentication/
trying to simulate a user logging into the normal web sight seems like hard work, Instagram supports OAuth 2.0 and if you search Nuget for OAuth you will find a whole load of packages to to the work for you.
for reference you can find twitter info at https://dev.twitter.com/docs
Tim
I figured it out, we actually need to send cookies to be able to logging, the code here for those people who have the same question:
Public cokkie As CookieContainer
Public Sub cookie()
'Dim webClient As New System.Net.WebClient
'Dim result As String = webClient.DownloadString("https://instagram.com/accounts/login")
Dim postData As String = ""
Dim tempCookies As New CookieContainer
Dim encoding As New ASCIIEncoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://instagram.com/accounts/login"), HttpWebRequest)
postReq.CookieContainer = tempCookies
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
cokkie = tempCookies
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
RichTextBox1.Text = thepage
TextBox1.Text = cokkie.ToString
Dim regx As New Regex("name=""csrfmiddlewaretoken"" value=""(.*?)""/>", RegexOptions.IgnoreCase)
Dim mactches As MatchCollection = regx.Matches(thepage)
For Each match As Match In mactches
TextBox1.Text = match.Value
TextBox1.Text = TextBox1.Text.Replace("name=""csrfmiddlewaretoken"" value=""", "")
TextBox1.Text = TextBox1.Text.Replace("""/>", "")
Next
End Sub
Public Sub log()
Dim postData As String = "csrfmiddlewaretoken=" & TextBox1.Text & "&username=xxxx&password=xxxx"
Dim tempCookies As New CookieContainer
Dim encoding As New ASCIIEncoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://instagram.com/accounts/login/"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = True
postReq.CookieContainer = cokkie
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "https://instagram.com/accounts/login/"
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0"
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)
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
RichTextBox1.Text = thepage
End Sub

HTTP Web Request

I am halfway done with this project. I need to login to my server at work, then submit a form on a different page. How do I keep the login connection alive then post a second form, or url?
Dim postData As String = "Username=system&Password=system&Action=Srh-1-1&Go=Login"
Dim tempCookies As New CookieContainer
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("MY SERVER URL"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = True
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "MY SERVER URL"
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
So, the login works fine. But, again, how do i now post to a different form, or load the entire form + submission details into a url? I need to post data after being logged in and submit the form. The form will return data I will then parse out. Basically, all of this can be done manually but this program will save me time by logging in, submitting, and parsing automatically.

Vb.net HTTPWEBREQUESTS GET method

i was trying to use the GET method instead of the POST method for my program which necessarily depends on the HTTPWEBREQUEST ..
here is the code i used
Dim postData As String = "GET /fbml/ajax/dialog/apprequests?message=Here%27s%20a%20Anvil%20for%20your%20pet%20in%20Wild%20Ones!%20Could%20you%20help%20me%20by%20sending%20a%20gift%20back%3F&data=[%7B%22request_type%22%3A%22gift_v2%22%2C%20%22gift%22%3A%22anvil%22%7D%2C%7B%22track%22%3A%22invite-Gift-request2-anvil-20120112-0%22%7D]&to=[%22100003034461289%22]&e2e=%7B%7D&app_id=101628414658&locale=en_US&sdk=joey&display=async&frictionless=true&redirect_uri=https%3A%2F%2Fwild-fb-apache-active-vip.playdom.com%2Fpub%2Fphp%2F&__d=1&__user=1668282525&__a=1&__dyn=7n8ahyj3419Aw&__req=3 HTTP/1.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("https://apps.facebook.com/fbml/ajax/dialog/apprequests?message=Here%27s%20a%20Anvil%20for%20your%20pet%20in%20Wild%20Ones!%20Could%20you%20help%20me%20by%20sending%20a%20gift%20back%3F&data=[%7B%22request_type%22%3A%22gift_v2%22%2C%20%22gift%22%3A%22anvil%22%7D%2C%7B%22track%22%3A%22invite-Gift-request2-anvil-20120112-0%22%7D]&to=[%22100003034461289%22]&e2e=%7B%7D&app_id=101628414658&locale=en_US&sdk=joey&display=async&frictionless=true&redirect_uri=https%3A%2F%2Fwild-fb-apache-active-vip.playdom.com%2Fpub%2Fphp%2F&__d=1&__user=1668282525&__a=1&__dyn=7n8ahyj3419Aw&__req=3"), HttpWebRequest)
postReq.Method = "GET"
postReq.KeepAlive = True
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-javascript; charset=utf-8"
postReq.Referer = "https://apps.facebook.com/fbml/ajax/dialog/apprequests?message=Here%27s%20a%20Anvil%20for%20your%20pet%20in%20Wild%20Ones!%20Could%20you%20help%20me%20by%20sending%20a%20gift%20back%3F&data=[%7B%22request_type%22%3A%22gift_v2%22%2C%20%22gift%22%3A%22anvil%22%7D%2C%7B%22track%22%3A%22invite-Gift-request2-anvil-20120112-0%22%7D]&to=[%22100003034461289%22]&e2e=%7B%7D&app_id=101628414658&locale=en_US&sdk=joey&display=async&frictionless=true&redirect_uri=https%3A%2F%2Fwild-fb-apache-active-vip.playdom.com%2Fpub%2Fphp%2F&__d=1&__user=1668282525&__a=1&__dyn=7n8ahyj3419Aw&__req=3"
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
WebBrowser1.DocumentText = thepage
You can use the webclient
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("url here")
Also the URL you used in your code looks incomplete

how to login to youtube and save cookies? vb.net

I have this code
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim logincookie As New CookieContainer
Dim postData As String = "continue=http%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26feature%3Dheader%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252F&service=youtube&uilel=3&dsh=-5804339713411277263&ltmpl=sso&hl=en_US&GALX=kpcgpuXEK94&pstMsg=1&dnConn=&checkConnection=youtube%3A4148%3A1&checkedDomains=youtube&timeStmp=&secTok=&Email=" & user_fb.Text & "&Passwd=" & pass_fb.Text & "&signIn=Sign+in&PersistentCookie=yes&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("https://accounts.google.com/ServiceLoginAuth"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = True
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "https://accounts.google.com/ServiceLoginAuth"
postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko)"
postReq.ContentLength = byteData.Length
Dim postreqstream As Stream = postReq.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
logincookie = tempCookies
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
WebBrowser1.DocumentText = thepage
End Sub
trying to make a tool for youtube upload video but it seems to have a cookies problem when I try to sign in !
it says that cookies must be allowed to run this action !
how can I enable cookies ?
Define one more Cookie container in the beginning:
Dim oRequestCookie As New CookieContainer
Try to add
CType(postReq, HttpWebRequest).CookieContainer = oRequestCookie
When you are defining the HttpWebRequest parameters.
And then at the end you can extract the login cookie as you already did.
Cheers.
The Problem is that your refer page should put some cookies in user machine
https://accounts.google.com/ServiceLoginAuth
then when your try to login google check for these cookies before you could login and as now google will find to cookies. The Solution is to access your refer page first and make sure to use CookieContainer in that page then call your code and it will work 100%.
Try to solve the problem logically then move to think about errors in your code.
...
Dim galx As New Cookie
galx.Name = "GALX"
galx.Value = "something"
galx.Domain = ".google.com"
Dim postData As String = "GALX=something&continue=http%3A%2F......
...
tempCookies.Add(galx)
I was searching exactly this "youtube login" thing. I got same message on webbrowser1 with "grudolf"s code. Then i looked firefox's "tools/developer console" while log in to youtube. There was a cookie collection with members "ACCOUNT_CHOOSER", "GALX", "GAPS" etc... When i add a cookie named "GALX" with any vale and add same into post data, result was OK. I just added code. I hope it will help...