日期:2014-05-18  浏览次数:21076 次

小高分求助,关于HttpWebResponse 返回
用C#做一个模拟登陆页面,不能返会响应后的页面

C# code

 String loginUrl = "http://localhost/yh/admin/login.asp";
 CookieContainer session = new CookieContainer();
 
 public void getLoginPage()
        {
            string strUsername = tb_username.Text.Trim();
            string strPassword = tb_password.Text.Trim();

            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "username=" + strUsername;
            postData += ("&password=" + strPassword);

            //postData = "username=steve&password=123456a%mofei=7049&submit=%C8%B7+%B6%A8";

            byte[] data = encoding.GetBytes(postData);

            // Prepare web request...   
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(loginUrl);                 

            myRequest.CookieContainer = session;

            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
            Stream newStream = myRequest.GetRequestStream();            
            // Send the data.   
            newStream.Write(data, 0, data.Length);       
            newStream.Close();

            // Get response   
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
            string content = reader.ReadToEnd();

            richTextBox1.Text = richTextBox1.Text + content;


        }



login.asp
HTML code
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档 </title>
</head>

<form name="form1" method="post" action="login_chk.asp">
<table>
    <tr>
      <td height="30"  colspan="2" align="center" bgcolor="#E8E8E8" class="td_2"> <b>管理登陆 </b> </td>
    </tr>
    <tr>
      <td width="80" align="center" class="td_2">用户名: </td>
      <td width="220" class="td_2"> <input type="text" name="username" class="input1" size="19"> </td>
    </tr>
    <tr>
      <td align="center" class="td_2">密&nbsp;&nbsp;码: </td>
      <td class="td_2"> <input type="password" name="password" class="input1" size="19"> </td>
    </tr> 
    <tr>
      <td height="30" colspan="2" align="center" valign="middle" class="td_2"> <input style="color: #FFFFFF; background-color: #FF1171; border: 1px solid #000000" type="submit" value="确 定" name="Submit">
&nbsp;&nbsp;&nbsp;
      <input style="color: #FFFFFF; background-color: #FF1171; border: 1px solid #000000" type="reset" value="取 消" name="