473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

HttpWebRequest - GET & POST In Single Request

300 256MB
Hey everyone,
What I'm trying to do is pass credentials to a website to log into it. I'm able to get an authenticity token and also POST a token with login information. Unfortunately, my code for getting the token via HttpWebResponse gets a token in one stream and closes, so that token is different than the new token required in my HttpWebRequest stream. What I need to do is utilize HttpWebRequest to get a token, not close the stream, then POST data. I have yet to see an example on search engines that matches this criteria.

I got this from a forum. It grabs the token string:
Expand|Select|Wrap|Line Numbers
  1.     Private Function getToken()
  2.         Dim r As HttpWebRequest = HttpWebRequest.Create("https://n135.meraki.com")
  3.         Dim re As HttpWebResponse = r.GetResponse()
  4.         Dim src As String = New StreamReader(re.GetResponseStream()).ReadToEnd()
  5.         Return (GetBetween(src, "<input name=""authenticity_token"" type=""hidden"" value=""", """ /></div> "))
  6.     End Function
This is the GetBetween() function used to grab the token within the getToken() function. Also from the same forum:
Expand|Select|Wrap|Line Numbers
  1.     Private Function GetBetween(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String, Optional ByVal Index As Integer = 0) As String
  2.         Return (Regex.Split(Regex.Split(Source, Str1)(Index + 1), Str2)(0))
  3.     End Function
This is the code in my button to try posting the login information to the web page:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  2.         Dim token As String = getToken()
  3.         Dim postData As String = "" 'formatted token string with username and password goes here
  4.         Dim tempCookies As New CookieContainer
  5.         Dim encoding As New UTF8Encoding
  6.         Dim byteData As Byte() = encoding.GetBytes(postData)
  7.         Dim r As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://n135.meraki.com"), HttpWebRequest)
  8.  
  9.         r.KeepAlive = True
  10.         r.Method = "POST"
  11.         r.CookieContainer = tempCookies
  12.         r.ContentType = "application/x-www-form-urlencoded"
  13.         r.Referer = "https://n135.meraki.com"
  14.         r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36"
  15.         r.ContentLength = byteData.Length
  16.  
  17.         Dim re As Stream = r.GetRequestStream()
  18.  
  19.         re.Write(byteData, 0, byteData.Length)
  20.         re.Close()
  21.  
  22.         Dim res As HttpWebResponse = DirectCast(r.GetResponse(), HttpWebResponse)
  23.  
  24.         tempCookies.Add(res.Cookies)
  25.         loginCookies = tempCookies
  26.  
  27.         Dim src As String = New StreamReader(res.GetResponseStream()).ReadToEnd()
  28.  
  29.         If (src.Contains("Welcome")) Then
  30.             loginCookies = tempCookies
  31.             MsgBox("Success!")
  32.         Else
  33.             tempCookies = Nothing
  34.             MsgBox("Failed.")
  35.         End If
  36.     End Sub
Any help would be greatly appreciated. I can convert from C# if someone has a working version or example similar to what I'm after. Thanks in advance!
Mar 15 '16 #1
0 1155

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: ME | last post by:
Hi; I am getting "Unhandled Exception: System.Net.WebException: The remote server returned an erro r: (401) Unauthorized." when I am trying to get a page via post. Code follows...
8
by: Du | last post by:
I'm trying to automate the upload process to yousendit.com, but the file size doesn't add up and yousendit.com keep rejecting my upload (it accepts the upload until the very end) I don't know...
0
by: Pmcg | last post by:
I would appreciate any help with the following, havn't found any answers for this in my research to date. I am trying to retreive a web page (a htm file intially) from a vdir on an intranet site...
3
by: Garnet2 | last post by:
I am trying to get the following code to work : HttpWebRequest request1 = (HttpWebRequest) WebRequest.Create(@"http://localhost/TacUssd/8.tac"); request1.Method = "POST"; request1.Credentials =...
2
by: Luis Esteban Valencia Muñoz | last post by:
I'm working on a web scraping application that needs to log into a website before it can get the data to scrape. I've always been confused about how the HttpWebRequest and HttpWebResponse objects...
16
by: Cheung, Jeffrey Jing-Yen | last post by:
I have a windows form application that generates a request, downloads an image, and waits the user to enter in login info. Unfortunately, this image is dynamic and based on session data. I have...
0
by: Fred Herring | last post by:
I currently use httpwebrequest to download large byte array files from my server. I am curious about how to change my httpwebrequest code to allow posing a large byte array to a virtual foldeer. ...
3
by: PawelR | last post by:
Hello Group, How send few value using HttpWebReguest with POST method in one request? Thx. PawelR
0
by: barrybevel | last post by:
Hi, I'm trying to login to the www.vodafone.ie website using HttpWebRequest. It works fine with IE/Firefox and the .NET Web Control too, just not with my code. I think it's a redirect 302...
0
by: andyc2002 | last post by:
Hi all, I have problem on use HttpWebRequest with "POST" method. First, I access http://store.apple.com/hk-zh/configu...=UNLOCKED%2FWW this link and go to the cart page. Then when I change the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.