PC Review


Reply
Thread Tools Rating: Thread Rating: 6 votes, 4.33 average.

Programmatically posting data to a website (Webrequest)

 
 
=?Utf-8?B?TWF0dA==?=
Guest
Posts: n/a
 
      23rd Mar 2004
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

 
Reply With Quote
 
 
 
 
Sherif ElMetainy
Guest
Posts: n/a
 
      23rd Mar 2004
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" <(E-Mail Removed)> wrote in message
news:2CD89788-E99D-4880-8303-(E-Mail Removed)...
> 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?
>
>
> Any suggestions or pointers deeply appreciate. Thanks a lot in advance for

all your help.
>
> Matt
>



 
Reply With Quote
 
 
 
 
Ravichandran J.V.
Guest
Posts: n/a
 
      23rd Mar 2004
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Hugo Wetterberg
Guest
Posts: n/a
 
      23rd Mar 2004
Hi Matt!
Take a look at this article.
http://www.codeproject.com/csharp/ms...?target=mshtml

I think that's what you're looking for

/Hugo

On Mon, 22 Mar 2004 22:26:06 -0800, "Matt"
<(E-Mail Removed)> wrote:

>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 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.GetEncoding("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?
>
>
>Any suggestions or pointers deeply appreciate. Thanks a lot in advance for all your help.
>
>Matt


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free website templates, website tools & website articles webguruaish@gmail.com Microsoft Frontpage 1 27th Apr 2007 12:01 PM
Posting Form Data locally and posting to Verisign =?Utf-8?B?SnVzdEJrdXp6?= Microsoft Frontpage 7 20th May 2006 06:47 PM
Programmatically posting form data to a web page and getting result in .net Ghafran Abbas Microsoft ASP .NET 0 12th Oct 2004 05:07 PM
Top-Posting vs. Bottom -Posting? Norman R. Cross Windows XP General 7 29th Sep 2003 04:21 AM
Changing Default Posting To Bottom Posting? Norman R. Cross Windows XP General 11 7th Aug 2003 01:08 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:18 PM.