posting daqta to an asp.net page from a console application

  • Thread starter Thread starter Ollie
  • Start date Start date
O

Ollie

I have an aspx page that contains a web control that contains 2 text boxes
and button to submit the text. I am trying to submit text and collect the
response from a console application, but it is not working correctly it is
just returning the original page I am expecting it to return the a
subsequent page (when a successfull login has occurred), the code is shown
below.

Does anyone know why it is not working correctly?

I used the Fiddler app to debug the HTTP header to work out the posting data
( nice app Eric :) )

Stream requestWriter = null;
string postData =
"_ctl1:username=ollie&_ctl1:password=XXXXX&_ctl1:SigninBtn.x=0&_ctl1:SigninB
tn.y=0";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byteData = encoding.GetBytes(postData);
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/XXXX/default.aspx");
objRequest.Method = "POST";
objRequest.ContentLength = postData.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";
objRequest.CookieContainer = new CookieContainer();

try
{
requestWriter = objRequest.GetRequestStream();
requestWriter.Write(byteData, 0, byteData.Length);
}
catch(Exception e)
{
System.Console.WriteLine(e.Message);
}
finally
{
requestWriter.Close();
}

HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
StreamReader responseReader = null;
responseReader = new StreamReader(objResponse.GetResponseStream());
string result = responseReader.ReadToEnd();

System.Console.WriteLine(result);
System.IO.StreamWriter fileWriter = null;
fileWriter =
System.IO.File.CreateText("c:\\work\\XXXX\\testpostdata.html");
fileWriter.Write(result);
fileWriter.Close();

System.Console.ReadLine();


Cheers and thanks in advance

Ollie
 
posting to an aspx.net page requires that you first do a Get and parse the
html for the viewstate value, so that you have a valid __Viewstate to
postback.

-- bruce (sqlwork.com)
 
Back
Top