peppersock
peppersock

Reputation: 11

VB.NET HttpWebRequest POST

Hey whhat's up. I've been looking at some example source codes but I can't quite figure it out. I'd like to send a POST request to login to a website with my account. For an example, how would I login to this website using HttpWebRequest..

http://z4.invisionfree.com/cotec/index.php?

It's for an application I'm building for my clan where you have to have an account on the forum to open the application, so if the login works it opens.

Upvotes: 1

Views: 14844

Answers (3)

Anonymous
Anonymous

Reputation: 21

dim email as string = "your email"
dim pass as string = "your pass"

    Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://touch.facebook.com"), HttpWebRequest)
                postReq.Method = "GET"
                postReq.KeepAlive = True
                postReq.CookieContainer = logincookie
                postReq.ContentType = "application/x-www-form-urlencoded"
                postReq.UserAgent = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C25 Safari/419.3"

                Dim postresponse As HttpWebResponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
                logincookie.Add(postresponse.Cookies)

                Dim postreqreader As New StreamReader(postresponse.GetResponseStream())

                Dim infos As String = postreqreader.ReadToEnd

                '---------------------------

                Dim byteData As Byte() = encoding.GetBytes("lsd=&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&version=1&ajax=0&width=0&pxr=0&gps=0&m_ts=&li=&email=" & email.Replace("@", "%40") & "&pass=" & pass & "&login=Connexion")

                postReq = DirectCast(WebRequest.Create("https://touch.facebook.com/login.php?refsrc=https%3A%2F%2Ftouch.facebook.com%2F&refid=8"), HttpWebRequest)
                postReq.Method = "POST"
                postReq.KeepAlive = True
                postReq.CookieContainer = logincookie
                postReq.ContentType = "application/x-www-form-urlencoded"
                postReq.Referer = "https://touch.facebook.com/"
                postReq.UserAgent = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C25 Safari/419.3"
                postReq.ContentLength = byteData.Length

                Dim postreqstream As Stream = postReq.GetRequestStream()
                postreqstream.Write(byteData, 0, byteData.Length)
                postreqstream.Close()

                postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
                logincookie.Add(postresponse.Cookies)

                postreqreader = New StreamReader(postresponse.GetResponseStream())

Upvotes: 2

SOL1102
SOL1102

Reputation: 512

There's a decent tutorial for HTTPWebRequest POST methods and a login example here: http://howtostartprogramming.com/vb-net/vb-net-tutorial-51-httpwebrequest-post-method/

Upvotes: 3

Keith Adler
Keith Adler

Reputation: 21178

Why not leverage this bit of code to simplify your effort: http://joel.net/code/easyhttp.aspx

Upvotes: 1

Related Questions