HTTP Web Request works for other sites, but not this one. Any ideas? (VB.NET)

1.3k views Asked by At

I'm writing a program in VB.NET, and I'm having trouble with my httpwebrequest code. I've used live HTTP headers for firefox to get the correct post data, but for some reason it won't login.

The thing is, I've used identical code for another similar site (changing just the post data) and it works just fine.

Any thoughts? Here is my code...

Imports System.Windows.Forms
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.ComponentModel
Imports System.Threading

Public Class Form1
        Dim logincookie As CookieContainer
        Dim html As String = "BGW still running"

        Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click

            Dim username As String = "username"
            Dim password As String = "password"

            Dim postData As String = "url=&username=" + username + "&password=" + password + "&login=Check+Mail%21"
            Dim tempcookies As New CookieContainer
            Dim encoding As New UTF8Encoding
            Dim bytedata As Byte() = encoding.GetBytes(postData)

            Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://www.website.com/inbox.aspx?login"), HttpWebRequest)
            postReq.Method = "POST"
            postReq.KeepAlive = True
            postReq.CookieContainer = tempcookies
            postReq.ContentType = "application/x-ww-form-urlencoded"
            postReq.Referer = "http://www.website.com/inbox.aspx?login"
            postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.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)
            tempcookies.Add(postResponse.Cookies)
            logincookie = tempcookies

            Dim matchCount As Integer = 0

        Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://www.website.com/editprofile.aspx")
            request.CookieContainer = logincookie
            Dim response As System.Net.HttpWebResponse = request.GetResponse
            Dim reader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())


            html = reader.ReadToEnd
    End Sub

End Class

Any thoughts or suggestions are appreciated.

2

There are 2 answers

1
starbolin On

Login is different on the URL you show. Is a redirect to a URL different than the one in your code.

0
AudioBubble On

Sorry for Later Reply, But this is going to help others as well..

Its because you are missing a CookieContainer which is very important for Logins using HTTP WEBREQUEST...

Here is an amaizing Tutorial! I am using the same method for login to MYBB FORUMS, vBULLETIN, and many other JOOMLA, WORDPRESS websites.. And been using the same code from the same tutorial over and over again..

Here is the Code:

Imports System.Net
Imports System.Text
Imports System.IO

Public Class Form1

Dim logincookie As CookieContainer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim postData As String = "referer=http%3A%2F%2Fforums.zybez.net%2Findex.php%3Fapp%3Dcore%26module%3Dglobal%26section%3Dlogin&username=" & TextBox1.Text & "&password=" & TextBox2.Text & "&rememberMe=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("http://forums.zybez.net/index.php?app=core&module=global&section=login&do=process"), HttpWebRequest)
postReq.Method = "POST"
postReq.KeepAlive = True
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/x-www-form-urlencoded"
postReq.Referer = "http://forums.zybez.net/index.php?app=core&module=global&section=login&do=process"
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())

End Sub

End Class

Credits to "HowToStartProgramming.Com"