By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

HttpWebRequest - GET & POST In Single Request

P: 299
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("")
  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(""), HttpWebRequest)
  9.         r.KeepAlive = True
  10.         r.Method = "POST"
  11.         r.CookieContainer = tempCookies
  12.         r.ContentType = "application/x-www-form-urlencoded"
  13.         r.Referer = ""
  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
  17.         Dim re As Stream = r.GetRequestStream()
  19.         re.Write(byteData, 0, byteData.Length)
  20.         re.Close()
  22.         Dim res As HttpWebResponse = DirectCast(r.GetResponse(), HttpWebResponse)
  24.         tempCookies.Add(res.Cookies)
  25.         loginCookies = tempCookies
  27.         Dim src As String = New StreamReader(res.GetResponseStream()).ReadToEnd()
  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
Share this question for a faster answer!
Share on Google+

Post your reply

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