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)
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)