此问题困扰两天了,郁闷得很,直接贴代码吧:
public CookieContainer Get_Login()
{
CookieContainer cc = new CookieContainer();
string FormURL = "http://localhost:1070/WebSite3/Login.aspx"; //处理表单的绝对URL地址
string FormData = "UserName=wq&Password=1"; //表单需要提交的参数,注意改为你已注册的信息。
byte[] data = Encoding.UTF8.GetBytes(FormData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FormURL);
request.Method = "POST"; //数据提交方式
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; TencentTraveler 4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; CIBA)";
//模拟一个UserAgent
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 WebContent = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
return cc;
}private void btnSubmit_Click(object sender, EventArgs e)
{
CookieContainer cc = new CookieContainer();
cc = Get_Login(); //获取登录Cookies string PhotoClassURL = "http://localhost:1070/WebSite3/Default.aspx";
HttpWebRequest Myrequest = (HttpWebRequest)WebRequest.Create(PhotoClassURL);
Myrequest.CookieContainer = cc;
HttpWebResponse Myresponse = (HttpWebResponse)Myrequest.GetResponse();
cc.Add(Myresponse.Cookies);
Stream Mystream = Myresponse.GetResponseStream();
string sHtml = new StreamReader(Mystream, System.Text.Encoding.UTF8).ReadToEnd();
txtSource.Text = sHtml;
}
cc = Get_Login(); 获取的cook数量总是为0,也就是没获取到不知道怎么回事 在Default.aspx页面中代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] == null)
{
Response.Redirect("Login.aspx");
return;
} if ((string)Session["UserName"] == string.Empty)
{
Response.Redirect("Login.aspx");
}
}
在Default.aspx页面中代码如下:
protected void btnSumbmit_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=.;database=LifeList;uid=sa;pwd=852527");
SqlCommand cmd =
new SqlCommand("select count(*) from tblUser where UserName='" + txtUserName.Text + "' and Password='" +
txtPassword.Text + "'", conn);
conn.Open();
if (Convert.ToInt32(cmd.ExecuteScalar()) == 1)
{
Session["UserName"] = "wq";
Response.Redirect("Default.aspx");
}
}
请教达人为什么取不到default.aspx的具体内容啊,登陆获取cookie那段是不是有问题,头都大了,谢谢
public CookieContainer Get_Login()
{
CookieContainer cc = new CookieContainer();
string FormURL = "http://localhost:1070/WebSite3/Login.aspx"; //处理表单的绝对URL地址
string FormData = "UserName=wq&Password=1"; //表单需要提交的参数,注意改为你已注册的信息。
byte[] data = Encoding.UTF8.GetBytes(FormData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FormURL);
request.Method = "POST"; //数据提交方式
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; TencentTraveler 4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; CIBA)";
//模拟一个UserAgent
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 WebContent = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
return cc;
}private void btnSubmit_Click(object sender, EventArgs e)
{
CookieContainer cc = new CookieContainer();
cc = Get_Login(); //获取登录Cookies string PhotoClassURL = "http://localhost:1070/WebSite3/Default.aspx";
HttpWebRequest Myrequest = (HttpWebRequest)WebRequest.Create(PhotoClassURL);
Myrequest.CookieContainer = cc;
HttpWebResponse Myresponse = (HttpWebResponse)Myrequest.GetResponse();
cc.Add(Myresponse.Cookies);
Stream Mystream = Myresponse.GetResponseStream();
string sHtml = new StreamReader(Mystream, System.Text.Encoding.UTF8).ReadToEnd();
txtSource.Text = sHtml;
}
cc = Get_Login(); 获取的cook数量总是为0,也就是没获取到不知道怎么回事 在Default.aspx页面中代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] == null)
{
Response.Redirect("Login.aspx");
return;
} if ((string)Session["UserName"] == string.Empty)
{
Response.Redirect("Login.aspx");
}
}
在Default.aspx页面中代码如下:
protected void btnSumbmit_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=.;database=LifeList;uid=sa;pwd=852527");
SqlCommand cmd =
new SqlCommand("select count(*) from tblUser where UserName='" + txtUserName.Text + "' and Password='" +
txtPassword.Text + "'", conn);
conn.Open();
if (Convert.ToInt32(cmd.ExecuteScalar()) == 1)
{
Session["UserName"] = "wq";
Response.Redirect("Default.aspx");
}
}
请教达人为什么取不到default.aspx的具体内容啊,登陆获取cookie那段是不是有问题,头都大了,谢谢
解决方案 »
- 时间转换问题!
- c# 控件透明
- 请问一个DataGridView多行数据保存到ArrayList然后读取出来的问题
- datagridview 焦点不改变就点工具栏的保存,Update(),没有更新到数据库,怎么解决
- DataGrid绑定CheckBoxList,求完整解答,高分送,不够再要
- 一个页面可以有多个FORMVIEW吗?
- label的image(gif)旋转后变成静止图像了,怎么半?
- 我用repeater绑定数据库了,现在想实现翻页功能
- 60高分求,循环执行同一个线程的问题
- linkbuton的TEXT属性,怎么换行?
- 具体错误在下面VS的代码中(大神们要不吝赐教啊+_!)
- [WinForm]winform程序如何终止和重启.
可以考虑用webservice啊,实现BS和CS的数据交互。
那要怎么做才可以突破这个登录,获取default.aspx源码?
if (Session["UserName"] == null)
{
Response.Redirect("Login.aspx");
return;
}
是这个造成的吧,请教
GET /WebSite3/Default.aspx HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
Referer: http://localhost:1070/WebSite3/Login.aspx
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; TencentTraveler 4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; CIBA)
Accept-Encoding: gzip, deflate
Host: localhost:1070
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: COOKIE=Language=2052; ASP.NET_SessionId=1m2s2tf0olvzs445v0h3fq45
为什么我获取不到这个cookie咧?
Cookie: COOKIE=Language=2052; ASP.NET_SessionId=1m2s2tf0olvzs445v0h3fq45,然后CookieContainer cc = new CookieContainer();
cc = Get_Login(); //获取登录Cookies string PhotoClassURL = "http://localhost:1070/WebSite3/Default.aspx";
HttpWebRequest Myrequest = (HttpWebRequest)WebRequest.Create(PhotoClassURL);
Myrequest.CookieContainer = cc;
访问defalut.aspx页面时就可以得到default.aspx页面的源码?
{
string outHtml = "";
try
{
byte[] data = Encoding.Default.GetBytes(strPost);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = rePpt.Method;
myRequest.ContentType = rePpt.ContentType;
myRequest.CookieContainer = CookieCtainer;
myRequest.Referer = rePpt.Referer;
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();
myResponse.Cookies = CookieCtion;
StreamReader reader;
if(coding == "UTF8")
reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
else
reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
outHtml = reader.ReadToEnd();
return outHtml;
}
catch (Exception ee)
{
outHtml = ee.Message;
return outHtml;
// MessageBox.Show(ee.Message);return outHtml;
}
}
rppt.Method = "POST";
rppt.Referer = "http://www.paynewline.com/";
rppt.Accept = " image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*";
rppt.ContentType = "application/x-www-form-urlencoded";
rppt.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)";
html = httpwebclient.Post("http://www.paynewline.com/business/sh_login.do", "dlip=&showmsg=null&dlfs=2&dlm=" + username + "&password=" + password + "&dynamicPwd=&imgnum=" + image, rppt, "UTF8");
public void SetCookieContainer(CookieCollection CookieCC)
{
CookieCtainer.Add(CookieCC);
}
public void SetCookieCollection(CookieCollection CookieCC)
{
CookieCtion=CookieCC;
}public string Post(string url, string strPost, RequestPPT rePpt, string coding)//string user, string password, string checkcode
{
string outHtml = "";
try
{
byte[] data = Encoding.Default.GetBytes(strPost);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = rePpt.Method;
myRequest.ContentType = rePpt.ContentType;
myRequest.CookieContainer = CookieCtainer;
myRequest.Referer = rePpt.Referer;
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();
myResponse.Cookies = CookieCtion;
StreamReader reader;
if(coding == "UTF8")
reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
else
reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
outHtml = reader.ReadToEnd();
return outHtml;
}
catch (Exception ee)
{
outHtml = ee.Message;
return outHtml;
// MessageBox.Show(ee.Message);return outHtml;
}
} public string GetCookieContainer(string url)//string user, string password, string checkcode
{
string outHtml = ""; try
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "GET";
req.CookieContainer = CookieCtainer;
HttpWebResponse wr = (HttpWebResponse)req.GetResponse(); StreamReader readwr = new StreamReader(wr.GetResponseStream(), Encoding.Default);
outHtml = readwr.ReadToEnd();
SetCookieCollection(wr.Cookies);
SetCookieContainer(wr.Cookies);
return outHtml;
}
catch (Exception ee)
{
outHtml = ee.Message;
return outHtml;
}
}httpwebclient.GetCookieContainer("http://www.paynewline.com/");这样先得到COOKIES
rppt.Method = "POST";
rppt.Referer = "http://www.paynewline.com/";
rppt.Accept = " image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*";
rppt.ContentType = "application/x-www-form-urlencoded";
rppt.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)";
html = httpwebclient.Post("http://www.paynewline.com/business/sh_login.do", "dlip=&showmsg=null&dlfs=2&dlm=" + username + "&password=" + password + "&dynamicPwd=&imgnum=" + image, rppt, "UTF8");
string formatString ="__VIEWSTATE={0}&txtUsername={1}&txtPassword={2}&btnSubmit={3}&__EVENTVALIDATION={4}";
string postString = string.Format(formatString, viewState, txtUsername, txtPassword,
btnSubmit, eventValidation);
但还是有点不明白,为什么一定要__VIEWSTATE,__EVENTVALIDATION等等,请大虾指教
用