Using Webrequest to log in and write source of another page to file?

Joined
Sep 3, 2011
Messages
1
Programming Experience
Beginner
Ok, here's the problem.
Im trying to make a webrequest with two functions.

The first function is to log in as a user. That works fine.
The second function is to stay logged in, and copy the source of another page on the site, to a text file.

When I try to make a new webrequest to save the source of the other page; it says that I have to be logged in.

I'm clueless as how to accomplish this. Can anyone give me an example?



Below is the code: So far - it only logs in and saves the source of the page you are redirected to after log-in.
I deleted the code from my failed attempt at getting the second function to work, because it was getting cluttered.


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

'Get Cookies
strURL = "http://www.blah.com/login.php/"
request = Net.HttpWebRequest.Create(strURL)
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
request.Method = "GET"
request.CookieContainer = cookieJar
response = request.GetResponse()
request.KeepAlive = False



For Each tmpCookie As Net.Cookie In response.Cookies
cookieJar.Add(tmpCookie)

Next

'Send the post data
request = Net.HttpWebRequest.Create(strURL)
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
request.Method = "POST"
request.AllowAutoRedirect = True
request.CookieContainer = cookieJar



Dim writer As StreamWriter = New StreamWriter(request.GetRequestStream())
writer.Write("email=email&pass=pass")
writer.Close()
response = request.GetResponse()





'Get the data from the page
Dim stream As StreamReader = New StreamReader(response.GetResponseStream())
Dim data As String = stream.ReadToEnd()

Dim streamwriter2 = New StreamWriter("c:/source.txt")
streamwriter2.WriteLine(data)
streamwriter2.Close()
 
Top Bottom