C#模拟登录
1.自己设计的一个登录页面,两个TextBox(txtName,txtPwd)框和一个Button,Button事件中设置(
protected void Button1_Click(object sender, EventArgs e)
{
if (txtName.Text == "fxl" && txtPwd.Text == "123")
{
Response.Redirect("Main.aspx");
}
}
{
if (txtName.Text == "fxl" && txtPwd.Text == "123")
{
Response.Redirect("Main.aspx");
}
}
)
2.添加一个控制台程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
using System.IO;
using System.Collections;
namespace 模拟登录
{
class Program
{
public static CookieContainer cc = new CookieContainer();
static void Main(string[] args)
{
CookieContainer cc = new CookieContainer();//this is for keep the Session and Cookie
Hashtable param = new Hashtable();//this is for keep post data.
//这个地址是我自己IIS上的地址
string urlLogin = "http://www.my.com/CS.aspx";
param.Add("txtName", "fxl");
param.Add("txtPwd", "123");
string result = PostAndGetHTML(urlLogin,ref cc, param);
Console.WriteLine(result);
}
public static string PostAndGetHTML(string targetURL,ref CookieContainer cc, Hashtable param)
{
string formData = "";
foreach (DictionaryEntry de in param)
{
formData += de.Key.ToString() + "=" + de.Value.ToString()+"&";
}
if(formData.Length>0)
formData = formData.Substring(0, formData.Length - 1); //remove last '&'
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(formData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);
request.Method = "POST"; //post
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)";
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
return result;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;
using System.IO;
using System.Collections;
namespace 模拟登录
{
class Program
{
public static CookieContainer cc = new CookieContainer();
static void Main(string[] args)
{
CookieContainer cc = new CookieContainer();//this is for keep the Session and Cookie
Hashtable param = new Hashtable();//this is for keep post data.
//这个地址是我自己IIS上的地址
string urlLogin = "http://www.my.com/CS.aspx";
param.Add("txtName", "fxl");
param.Add("txtPwd", "123");
string result = PostAndGetHTML(urlLogin,ref cc, param);
Console.WriteLine(result);
}
public static string PostAndGetHTML(string targetURL,ref CookieContainer cc, Hashtable param)
{
string formData = "";
foreach (DictionaryEntry de in param)
{
formData += de.Key.ToString() + "=" + de.Value.ToString()+"&";
}
if(formData.Length>0)
formData = formData.Substring(0, formData.Length - 1); //remove last '&'
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(formData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);
request.Method = "POST"; //post
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)";
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
request.CookieContainer = cc;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream = response.GetResponseStream();
string result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
return result;
}
}
}
问题1.但是获取的源代码总是登录页面的,请大家看看怎么才能获得登录后页面的源代码
2.POST数据用不用加上Button1这个参数,我是使用Firefox中的firebug获取页面的POST信息的
问题补充: 一楼您好,我又添加了 request.AllowAutoRedirect = true; 但是还是获得不了,使用HttpWebRequest时POST中的参数 要几个啊? 1. txtName txtPwd ?(Button 和EVENTVALIDATION 和VIEWSTATE需要不)
HttpWebRequest默认好像是不支持Redirect的,你需要设置AllowAutoRedirect为true才能取到跳转后的页面源码。
如果你的代码是写在button的click事件中的话,可能还需要传递一些asp.net自动生成的viewstate数据,你可以用firebug查看一下点击按钮后提交了哪些数据。
建议你直接判断页面的请求方式来做登录,比如:
if (Request.RequestType == "POST") {
if (Request.Form["txtName"] == "fxl" && Request.Form["txtPwd"] == "123") {
Response.Redirect("Main.aspx");
}
}
if (Request.Form["txtName"] == "fxl" && Request.Form["txtPwd"] == "123") {
Response.Redirect("Main.aspx");
}
}
这样就不用考虑viewstate的问题。
posted on 2013-04-25 11:55 HOT SUMMER 阅读(372) 评论(0) 编辑 收藏 举报