用C#做一个模拟登陆页面,不能返会响应后的页面
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<!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">密 码:</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">
<input style="color: #FFFFFF; background-color: #FF1171; border: 1px solid #000000" type="reset" value="取 消" name="Submit2"></td>
</tr>
</table>
</form><body>
</body>
</html>
login_chk.asp<%
if Request("username") ="1" and Request("password") = "1" then
Response.Write("用户登陆成功")
else
Response.Write ("请输入正确的用户名和密码")
end if
%>
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<!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">密 码:</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">
<input style="color: #FFFFFF; background-color: #FF1171; border: 1px solid #000000" type="reset" value="取 消" name="Submit2"></td>
</tr>
</table>
</form><body>
</body>
</html>
login_chk.asp<%
if Request("username") ="1" and Request("password") = "1" then
Response.Write("用户登陆成功")
else
Response.Write ("请输入正确的用户名和密码")
end if
%>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货