views:

443

answers:

2

I've hit a wall on POST'ing data to a webform :( Below is the code I've adapted from many places (here most recently: http://p2p.wrox.com/asp-net-1-0-1-1-professional/34517-webrequest-webresponse-form-submit-problem-help.html )

Dim url As String = "https://student.ashford.edu/student/"
        Dim data As String = "username=myusername&password=mypassword"
        Dim buffer As Byte() = Encoding.UTF8.GetBytes(data)
        Dim result As String = ""
        Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        req.Method = "POST"
        req.ContentType = "application/x-www-form-urlencoded"
        req.ContentLength = buffer.Length
        req.CookieContainer = New CookieContainer()
        ' enable cookies
        Dim reqst As Stream = req.GetRequestStream()
        ' add form data to request stream
        reqst.Write(buffer, 0, buffer.Length)
        reqst.Flush()
        reqst.Close()

        Console.WriteLine(vbLf & "Posting data to " & url)
        Dim res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
        ' send request,get response
        Console.WriteLine(vbLf & "Response stream is: " & vbLf)
        Dim resst As Stream = res.GetResponseStream()
        ' display HTTP response
        Dim sr2 As New StreamReader(resst)
        result = sr2.ReadToEnd()
        Using writer As System.IO.StreamWriter = New StreamWriter("C:\Temp\checkcheck.html")
            writer.Write(result)
        End Using

When I check the checkcheck.html file, it doesn't show the page I normally see when I've successfully logged into my university, but instead shows the login page again prompting for a user name and password. Since it's prompting for a user/pass in the output response, does that mean nothing is posted? Or is the login button not being hit?

As far as the field names in the source, the name of the username field is "username", and the name of the password field is "password". From what I can tell, the name of the login button itself is "submit". Below is the source of the page, am I missing something?

view-source:https://student.ashford.edu/student/
https://student.ashford.edu/student/

What I want to have happen is that in my VB.NET backend, I click a button and my username and password are automatically entered and I am automagically logged in. Once the login is done, I want to retrieve the HTML of the logged in page so I can parse it for items relating to my studies. This may be unrelated, but do I need cookies to keep my application "logged in" if I navigate to other pages?

EDIT: I tried plugging different URL's, to no avail. I have also tried adding submit=login and login=submit (alternately of course) to the POST data. I'm still getting the original login screen HTML from the result variable at the end, instead of my university homepage that I'm expecting after I normally login by hand as a human would. Maybe there is a follow up step that I am missing?

A: 

The actual form action is at https://student.ashford.edu/login/commands/login.php. You might plug that into url instead. Also, try including the submit button - submit=login - in the values.

Daniel A. White
Is that where I would POST the data to? As in, I would set my 1st URL variable to that address? I just plugged that address into the URL variable, and I get the same result of dumping me back at the login page...
Bill Sambrone
A: 

I found out what I needed to do. First I have to make sure that I have a cookie container. Next, I used Fiddler to find out EXACTLY what is being sent back and forth between my browser and where I want to login. I post all the login info, and I get the response back. This seems to fill up my cookie container variable with the required authentication cookies? Next I make another POST request to the page I want to go to, and voila - it works.

Bill Sambrone