G
Guest
Hello
I am trying to write an application (Win forms with C#) that logs into a website (username & password) and after authenticated, navigates to a specific page by following a couple links and retrieve some data from the page. This will be all done programmatically. The user will only input username and password and the next thing he/she will see is the information that the program retrieves
This is what I do
<PROGRAM
//1) Initial request for the login page (HTTP GET
string url = "http://www.someserver.com/login.aspx"
HttpWebRequest hRqst = (HttpWebRequest) WebRequest.Create(url)
hRqst.CookieContainer = new CookieContainer()
StreamReader strRdr = new StreamReader(hRqst.GetResponse().GetResponseStream(),System.Text.Encoding.GetEncoding("utf-8"))
//read respons
strRdr.ReadToEnd()
strRdr.Close()
//2) Now the request with login data attached(HTTP POST
HttpWebRequest hRqst1 = (HttpWebRequest) WebRequest.Create(url)
hRqst1.CookieContainer = new CookieContainer()
//post dat
StringBuilder str = new StringBuilder()
str.Append("userName=test&password=testpw")
byte [] postData = Encoding.ASCII.GetBytes(str.ToString())
//set cookie
HttpWebResponse httpResp = (HttpWebResponse) hRqst.GetResponse(); //to get any cookies from the initial response from
hRqst1.CookieContainer.Add(httpResp.Cookies)
//heade
hRqst1.Method = "POST"
hRqst1.ContentType = "application/x-www-form-urlencoded"
hRqst1.ContentLength = postData.Length
Stream strm = hRqst1.GetRequestStream()
strm.Write(postData,0,postData.Length)
strm.Close()
//3) Read the response to the pos
strRdr = new StreamReader(hRqst1.GetResponse().GetResponseStream(),System.Text.Encoding.GetEncoding("utf-8"))
//display the respons
this.txtInfo.Text = strRdr.ReadToEnd()
strRdr.Close()
strRdr = null
</PROGRAM
Problem: The response that I get to my post with login credentials in step 2 is the same as the response to the initial request (HTTP GET). It is as if the login data that I sent is simply ignored. I do not get any error messages (HTTP) or program messages from the server side such as invalid username/password when I change the data. Also what happens if the HTTP header does not include data from other controls on the page (only a subset of values from page controls) ? For examples, the aspx page has a hidden control for the view state but I do not send this data back to the server with the login credentials
Any suggestions or pointers deeply appreciate. Thanks a lot in advance for all your help
Mat
I am trying to write an application (Win forms with C#) that logs into a website (username & password) and after authenticated, navigates to a specific page by following a couple links and retrieve some data from the page. This will be all done programmatically. The user will only input username and password and the next thing he/she will see is the information that the program retrieves
This is what I do
<PROGRAM
//1) Initial request for the login page (HTTP GET
string url = "http://www.someserver.com/login.aspx"
HttpWebRequest hRqst = (HttpWebRequest) WebRequest.Create(url)
hRqst.CookieContainer = new CookieContainer()
StreamReader strRdr = new StreamReader(hRqst.GetResponse().GetResponseStream(),System.Text.Encoding.GetEncoding("utf-8"))
//read respons
strRdr.ReadToEnd()
strRdr.Close()
//2) Now the request with login data attached(HTTP POST
HttpWebRequest hRqst1 = (HttpWebRequest) WebRequest.Create(url)
hRqst1.CookieContainer = new CookieContainer()
//post dat
StringBuilder str = new StringBuilder()
str.Append("userName=test&password=testpw")
byte [] postData = Encoding.ASCII.GetBytes(str.ToString())
//set cookie
HttpWebResponse httpResp = (HttpWebResponse) hRqst.GetResponse(); //to get any cookies from the initial response from
hRqst1.CookieContainer.Add(httpResp.Cookies)
//heade
hRqst1.Method = "POST"
hRqst1.ContentType = "application/x-www-form-urlencoded"
hRqst1.ContentLength = postData.Length
Stream strm = hRqst1.GetRequestStream()
strm.Write(postData,0,postData.Length)
strm.Close()
//3) Read the response to the pos
strRdr = new StreamReader(hRqst1.GetResponse().GetResponseStream(),System.Text.Encoding.GetEncoding("utf-8"))
//display the respons
this.txtInfo.Text = strRdr.ReadToEnd()
strRdr.Close()
strRdr = null
</PROGRAM
Problem: The response that I get to my post with login credentials in step 2 is the same as the response to the initial request (HTTP GET). It is as if the login data that I sent is simply ignored. I do not get any error messages (HTTP) or program messages from the server side such as invalid username/password when I change the data. Also what happens if the HTTP header does not include data from other controls on the page (only a subset of values from page controls) ? For examples, the aspx page has a hidden control for the view state but I do not send this data back to the server with the login credentials
Any suggestions or pointers deeply appreciate. Thanks a lot in advance for all your help
Mat