views:

616

answers:

1

I am looking for an application that can do the following

a) Programmatically auto login to a page(login.asxp) using HttpWebResponse by using already specified username and password.

b) Detect the redirect URL if the login is successful.

c) Submit another form (settings.aspx) to update certain fields in the database.

The required coding needs to be using asp.net

The application needs to complete this entire process in the same session cookie.

A: 
string sUrl = "login.aspx";
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(sUrl);
CookieContainer oMyCookies = new CookieContainer();
oRequest.CookieContainer = oMyCookies;
// encode postdata into byte array.  the postdata string format will most likely be different and you'll have to examine the postdata going back and forth using some firefox addon like LiveHTTPHeaders
byte[] oPostData = System.Encoding.UTF8.GetBytes("username=" + HttpUtility.UrlEncode(sUser) + "&pass=" HttpUtility.UrlEncode(sPass));
using (Stream oStream = oRequest.GetRequestStream())
{
    oStream.Write(oPostData, 0, oPostData.Length);
}
HttpWebResponse oResponse = oRequest.GetResponse();
// save response cookies in our cookie object for future sessions!
foreach (Cookie oCookie in oResponse.Cookies)
{
    oMyCookies.SetCookies(sUrl, oCookie.ToString());
}
// maybe check response headers for location
string sResponseContents = null;
using (StreamReader oReader = new StreamReader(oResponse.GetResponseStream())
{
    // store server response into string
    sResponseContents = oReader.ReadToEnd();
}

...this is the basic code required for what you want to do.

marduk