Programmatically posting data to a website (Webrequest)

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
 
S

Sherif ElMetainy

Hello

I see the example url you use is aspx.
Many aspx pages use __VIEWSTATE hidden field to detemine if data is posted
to the page. The __VIEWSTATE date has to be validated for security.
This might be the problem. If this is the problem, then you have to parse
the html returned by the login page to get the value of the __VIEWSTATE
hidden field, then append it to the posted data.

Best regards,
Sherif

Matt said:
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.Ge
tEncoding("utf-8"));
//read response
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 data
StringBuilder str = new StringBuilder();
str.Append("userName=test&password=testpw");
byte [] postData = Encoding.ASCII.GetBytes(str.ToString());
//set cookies
HttpWebResponse httpResp = (HttpWebResponse) hRqst.GetResponse(); //to
get any cookies from the initial response from 1
hRqst1.CookieContainer.Add(httpResp.Cookies);
//header
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 post
strRdr = new StreamReader(hRqst1.GetResponse().GetResponseStream(),System.Text.Encoding.G
etEncoding("utf-8"));
//display the response
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?
 
R

Ravichandran J.V.

I am not sure if this answers your question. To persist values across
postbacks you need to place the values specifically within the ViewState
Bag.

eg.
ViewState("myValues")=123344.

BUT, since ViewState is a property of the Page class, it may not be
accessible with the code that you are trying, anyway, I don't really
understand what your problem is.


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 

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