HttpWebRequest POST problem

R

Robert Avery

I am trying to log on to a website in order to automatically download
its files. I used to use a VB6 program that used the WebBrowser
control, but for some reason that no longer works. Now, I am trying a
VB.NET solution, and I have been encountering some road blocks.

The website requires that you enter a username and password in its
fields, and then click the login button. I have tried to use the
below code to POST the username/password , but when I execute the
request and receive the response, it does not return the response you
would get if you did it manually in your browser. Instead, it merely
returns the login page with the username/password filled in.

Any help would be appreciated!



Dim uri As New Uri("https://www.ctslink.com/login.do")
Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
Dim strData As String =
"userid=<myusername>&password=<mypassword>"
request.UserAgent = "Mozilla/4.0"
request.ContentType = "application/x-www-form-urlencoded"

request.AllowAutoRedirect = True
request.Method = WebRequestMethods.Http.Post
request.UseDefaultCredentials = True

Dim UTF8 As System.Text.Encoding = New
System.Text.UTF8Encoding
Dim b() As Byte = UTF8.GetBytes(strData)
request.ContentLength = b.Length


Dim requestStream As Stream = request.GetRequestStream()


requestStream.Write(b, 0, b.Length)

Dim response As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(response.GetResponseStream())
Dim tmp As String = reader.ReadToEnd()
response.Close()
File.WriteAllText("c:\hoho.txt", tmp)
 
R

Robert Avery

I am using VS2008 with framework 3.5. The C# code in the example is
substantially similar to mine, and it results in the same thing as my
code. It is simply returning the same URL with the login information
filled in. It doesn't actually log in to the website. Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top