locked
C# HttpWebRequest with login page and then go to another page RRS feed

  • Question

  • My problem is when I want to go to index.aspx. I mus pass login.aspx by user username and password and i have this code :

    CookieContainer cookieContainer = new CookieContainer();
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://127.0.0.1/login.aspx");
    request.CookieContainer=cookieContainer;
     request.Method = "POST";
     string postData = "vaUsrCde=username&vaPwdpassword&vaAction=Login";
     byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    request.ContentType = "application/x-www-form-urlencoded";
     request.ContentLength = byteArray.Length;
    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    request = (HttpWebRequest)HttpWebRequest.Create("http://127.0.0.1/page2.aspx");
    request.Method = "POST";//Because when i use "GET" it have error.
    request.CookieContainer = cookieContainer;
    dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();

    WebResponse response = request.GetResponse();
    Console.WriteLine(((HttpWebResponse)response).StatusDescription);
    dataStream = response.GetResponseStream(); <=== Error 500
    StreamReader reader = new StreamReader(dataStream,Encoding.GetEncoding("Windows-874"));
    string responseFromServer = reader.ReadToEnd();
    Console.WriteLine(responseFromServer);


    xcover
    • Moved by Edwer Fang Tuesday, December 29, 2009 2:43 AM (From:Visual Studio Team System - Testing)
    Wednesday, December 23, 2009 2:42 AM

Answers

All replies