VB.net HttpWebRequest 403 error - vb.net

My code is 100% working but when I try filling up the forms under yelp.com it gives me 403 error. Here is my code:
Dim cweb As String = "http://www.yelp.com/biz_share?bizid=T6XCD1_eLEk3LaSp8C7E1g&return_url=%2Fbiz%2Fmr-c-los-angeles-2"
Dim POST As String = "csrftok=6cc5dea3ff8bf8f404f1e7a4951342cb2f132a17cb625a3988c50027d358285d&context=pyZQEaHS1YbhP3EEsTKGww&action_submit=1&emails=samplemail#gmail.com&emails=&emails=&unauth_name=Test+Name&unauth_email=testemail%40email.com&note=How%27s+it+going%3F"
Dim request As HttpWebRequest
Dim response As HttpWebResponse
Dim tempCookies As New CookieContainer
request = CType(WebRequest.Create(cweb), HttpWebRequest)
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0"
request.AllowAutoRedirect = True
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = POST.Length
request.Method = "POST"
request.KeepAlive = True
request.CookieContainer = tempCookies
Dim requestStream As Stream = request.GetRequestStream()
Dim postBytes As Byte() = Encoding.ASCII.GetBytes(POST)
requestStream.Write(postBytes, 0, postBytes.Length)
requestStream.Close()
response = CType(request.GetResponse(), HttpWebResponse)
tempCookies.Add(response.Cookies)
Dim postreader As New StreamReader(response.GetResponseStream())
Dim thepage As String = postreader.ReadToEnd
response.Close()
The web form I am basing is this:
http://www.yelp.com/biz_share?bizid=T6XCD1_eLEk3LaSp8C7E1g&return_url=%2Fbiz%2Fmr-c-los-angeles-2
On other web forms I am able to fill-up and send them, does this mean that yelp.com won't let you send any webrequest? I am really confused right now. Any help will be gladly accepted thanks in advance.

The csrftok will change for each user / session, the 403 is there because your authentication data is bad / the csrftok is not valid for the session. You need to go to the page before this (Or a login page or similar) to get the correct token.

Related

VB.NET webrequest returning 404 when the url is correct

I have been at this for hours trying everything. I am porting this code from an Access 2010 project to vb.net I have no idea what could be going wrong here.
Visual Studio 2019, Framework 4.7.2
Dim data As Byte() = Encoding.UTF8.GetBytes(strBody)
url = "https://rest.avatax.com/api/v2/companies/" & OE2019HomeForm.strAvalaraCompany & "/certificates/"
Dim uri As Uri = New Uri(url)
Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(uri)
request.Method = "Post"
request.Headers.Add("40", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)")
request.Headers.Add("Authorization", "Basic " & OE2019HomeForm.strAuth)
request.Headers.Add("Cache-Control", "no-cache")
request.Headers.Add("20", "application/json") 'this is the accept header
request.Headers.Add("12", "text/plain") ' content type
request.ContentLength = data.Length
Dim stream As Stream = request.GetRequestStream()
stream.Write(data, 0, data.Length)
stream.Close()
Using response As WebResponse = request.GetResponse()
Using responsestream As Stream = response.GetResponseStream()
Using reader As StreamReader = New StreamReader(responsestream)
Dim webresponse As String
webresponse = reader.ReadToEnd()
MsgBox(webresponse)
End Using
End Using
End Using
' if I add line below before dimming the request I get "'The underlying connection was closed: An unexpected error occurred on a receive.' Inner Exception: Win32Exception: The client and server cannot communicate, because they do not possess a common algorithm"
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
any help would be fantastic.
Does this work for you?
Put this near the top.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or
SecurityProtocolType.Tls12 Or SecurityProtocolType.Ssl3 Or SecurityProtocolType.Ssl3
ServicePointManager.ServerCertificateValidationCallback = Function(obj As [Object],
certificate As X509Certificate,
chain As X509Chain,
errors As SslPolicyErrors) (True)

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

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 detecting a failed HttpWebRequest

I have here a working HttpWebRequest code but my problem is it still keeps on doing the WebRequest even if the website I will specify is offline, which means it still keeps on making webrequests even though the request never really happened in the first place.
Here is my code:
Dim cweb As String = "http://samplewebsiteform.com"
Dim POST As String = "name=TestName&age=50"
Dim request As HttpWebRequest
request = CType(WebRequest.Create(cweb), HttpWebRequest)
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36"
request.AllowAutoRedirect = False
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = POST.Length
request.Method = "POST"
request.KeepAlive = False
request.Timeout = 500
Dim requestStream As Stream = request.GetRequestStream()
Dim postBytes As Byte() = Encoding.ASCII.GetBytes(POST)
requestStream.Write(postBytes, 0, postBytes.Length)
requestStream.Close()
How do i trap this webrequest whenever it tries to make a webrequest with an offline website so that it would stop making the request?
You are not getting the Response before requesting the request stream.
This line:
Dim resphttp As HttpWebResponse = CType(HttpWebResponse, request.GetResponse)
Will allow you to get the web response status code (404 not found, 500 error...)
If resphttp.StatusCode <> Net.HttpStatusCode.Accepted Then
'There was an error
End If
And after requesting the response the you get the requestStream:
Dim requestStream As Stream = request.GetRequestStream()

HTTPWebRequest Login POST is not Redirecting

I need to use HTTPWebRequest to login to an external website and redirect me to the default page. My code below is behind a button - when clicked it currently tries to do some processing but stays on the same page. I need it to redirect me to the default page of the external website without seeing the login page. Any help on what I'm doing wrong?
Dim loginURL As String = "https://www.example.com/login.aspx"
Dim cookies As CookieContainer = New CookieContainer
Dim myRequest As HttpWebRequest = CType(WebRequest.Create(loginURL), HttpWebRequest)
myRequest.CookieContainer = cookies
myRequest.AllowAutoRedirect = True
myRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1"
Dim myResponse As HttpWebResponse = CType(myRequest.GetResponse(), HttpWebResponse)
Dim responseReader As StreamReader
responseReader = New StreamReader(myResponse.GetResponseStream())
Dim responseData As String = responseReader.ReadToEnd()
responseReader.Close()
'call a function to extract the viewstate needed to login
Dim ViewState As String = ExtractViewState(responseData)
Dim postData As String = String.Format("__VIEWSTATE={0}&txtUsername={1}&txtPassword={2}&btnLogin.x=27&btnLogin.y=9", ViewState, "username", "password")
Dim encoding As UTF8Encoding = New UTF8Encoding()
Dim data As Byte() = encoding.GetBytes(postData)
'POST to login page
Dim postRequest As HttpWebRequest = CType(WebRequest.Create(loginURL), HttpWebRequest)
postRequest.Method = "POST"
postRequest.AllowAutoRedirect = True
postRequest.ContentLength = data.Length
postRequest.CookieContainer = cookies
postRequest.ContentType = "application/x-www-form-urlencoded"
postRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1"
Dim newStream = postRequest.GetRequestStream()
newStream.Write(data, 0, data.Length)
newStream.Close()
Dim postResponse As HttpWebResponse = CType(postRequest.GetResponse(), HttpWebResponse)
'using GET request on default page
Dim getRequest As HttpWebRequest = CType(WebRequest.Create("https://www.example.com/default.aspx"), HttpWebRequest)
getRequest.CookieContainer = cookies
getRequest.AllowAutoRedirect = True
Dim getResponse As HttpWebResponse = CType(getRequest.GetResponse(), HttpWebResponse)
'returns statuscode = 200
FYI - when i add in this code at the end, i get the HTML of the default page I'm trying to redirect to
Dim responseReader1 As StreamReader
responseReader1 = New StreamReader(getRequest.GetResponse().GetResponseStream())
responseData = responseReader1.ReadToEnd()
responseReader1.Close()
Response.Write(responseData)
Any help on whats missing to get the redirect working?
Cheers
The HttpWebRequest only automatically redirects you if the server sends an HTTP 3xx redirection status with a Location field in the response. Otherwise you are supposed to manually navigate to the page by using Response.Redirect, for example. Also keep in mind that the automatic redirection IGNORES ANY COOKIES sent by the server. That may be the problem in your case if the server is actually sending a redirection status.