Ignore response errors VB.NET - vb.net

I have a problem with getting response, when I am getting response like 400 error or 403 error, poping up unexpected event error.
My question is how to ignore these errors?
Here is the code:
Dim postData As String = "{""agent"": {""name"": ""Minecraft"",""version"": 1},""username"": " + TextBox1.Text + ",""password"": " + TextBox2.Text + ",""clientToken"": ""client identifier""}"
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://authserver.mojang.com/authenticate"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = True
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/json"
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
postReq.UseDefaultCredentials = True
postReq.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials
postReq.Credentials = CredentialCache.DefaultCredentials
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
If thepage.Contains(":true") Then
MsgBox("Valid")
End If
If thepage.Contains("ForbiddenOperationException") Then
MsgBox("Not Valid")
End If

Around each statement for which you wish to ignore exceptions, place a try/catch where the 'catch' is empty:
Try
... single statement
Catch
//empty
End Try
However, are you sure you want to just ignore exceptions?

Sub test()
On Error GoTo ErrHand
'Code here
Exit Sub
ErrHand:
Msgbox("You got the error 91")
If Err.Number = 91 Then Goto Errhand
End Sub

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

Instagram Register HttpWebRegister

I want to make a method to create an Instagram, but I can't to figure out how to get the cookie automatically
My old method :
Function register()
Dim postData As String = "email=" & EmailBox.Text + "#gmail.com" & "&password=" & PassBox.Text & "&username=" & UserBox.Text & "&first_name=" & NameBox.Text
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim request As HttpWebRequest = DirectCast(WebRequest.Create("https://www.instagram.com/accounts/web_create_ajax/"), HttpWebRequest)
request.Method = "POST"
request.KeepAlive = True
request.Headers.Add("X-CSRFToken", "q4NBIiEDDiPwGliNk6xxxxW9IooCHm")
request.ContentType = "application/x-www-form-urlencoded"
request.Headers.Add("X-Instagram-AJAX", "1")
request.Headers.Add("X-Requested-With", "XMLHttpRequest")
request.Headers.Add("Cookie", "mid=WZWf8QALxxxxcWsM-P1hm5F; csrftoken=q4NBIiEDDixxxxZrW9IooCHm; rur=FTW; ig_vw=16; ig_pr=1; ig_vh=4")
request.Referer = "https://www.instagram.com/"
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"
request.ContentLength = byteData.Length
Dim requestStream As Stream = request.GetRequestStream()
requestStream.Write(byteData, 0, byteData.Length)
requestStream.Close()
Dim responseS As HttpWebResponse
responseS = DirectCast(request.GetResponse(), HttpWebResponse)
Dim Reader As New StreamReader(responseS.GetResponseStream())
Dim thepage As String = Reader.ReadToEnd
If thepage.Contains("This username isn't available. Please try another") Then
Label6.ForeColor = Color.DarkRed
Label6.Text = UserBox.Text + "Has been fail register !!"
Else
Label6.ForeColor = Color.LightGreen
Label6.Text = UserBox.Text + "Has been successfully register !!"
End If
TextBox1.Text = thepage
End Function
How can I get the cookie automatically?
there you go. This is an sample for login in to ASP.NET MVC website. Look for Cookiejar to retreive them.
Dim tclient As HttpClient
Dim cookiejar As New CookieContainer
cookiejar = handler.CookieContainer
Dim xResponse As HttpResponseMessage = Await (tclient.GetAsync(BaseAddress & LoginUrl))
Dim xcookies As CookieCollection = cookiejar.GetCookies(New Uri(BaseAddress & LoginUrl))
For Each tCookie As Cookie In xcookies
If tCookie.Name = "__RequestVerificationToken" Then vCookie = tCookie : VerificationToken = tCookie.Value : Exit For
Next
Dim body As String = Await (xResponse.Content.ReadAsStringAsync())
Dim htdoc As New HtmlAgilityPack.HtmlDocument
htdoc.LoadHtml(body)
Dim xxdoc As HtmlNode = htdoc.GetElementbyId("loginform")
For Each xnodes As HtmlNode In xxdoc.Elements("input")
If xnodes.Attributes(0).Value = "__RequestVerificationToken" Then
FormValidationToken = xnodes.Attributes(2).Value
End If
Next

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

how to make a GET httpwebrequest in vb.net

i was trying to make a form navigating program and i wanted to know how to make a GET httpwebrequest here is the code i use for a POST 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
You can change the Method to "GET" and drop the request stream:
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 = "GET"
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"
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

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