模拟登录后,redirect不成功的问题

selvn 2009-12-12 11:13:31
想模拟登录开心网,然后直接redirect过去, 详看代码

protected void Button1_Click(object sender, EventArgs e)
{
string postData = "url=/home/&email=xxx&password=xxx&remember=1";
byte[] byteRequest = Encoding.Default.GetBytes(postData);

HttpWebRequest httpWebRequest;
string url = " http://www.kaixin001.com/login/login.php?";
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
CookieContainer cc = new CookieContainer();
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.KeepAlive = true;
httpWebRequest.CookieContainer = cc;

httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = byteRequest.Length;

Stream stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();

HttpWebResponse httpWebResponse;
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();

Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();

//Response.Write(html); //这里写出来的html是登录后的html,证明是成功的
Response.Redirect("http://www.kaixin001.com/home");//问题就在这里,redirect过去后就是未登录的状态, 没法转到主页,初步研究后怀疑是cookies没有设置成功,
}


各位大大看看如何才能登录成功,谢谢!
...全文
170 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
JackLeiMM 2010-01-18
  • 打赏
  • 举报
回复
cookie要同步啊

CookieContainer cc = new CookieContainer();
string postData = "NetBarID=" + this.txtbarNumber.Text + "&PCIP=" + this.txttmp.Text;
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri("http://211.139.163.27/NetDesktopWeb/Aspx/Ref.aspx?NetBarID=" + this.txtbarNumber.Text + "&PCIP=" + this.txttmp.Text));
webRequest2.CookieContainer = cc;
webRequest2.Method = "POST";
webRequest2.ContentType = "application/x-www-form-urlencoded";
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream();
// Send the data.
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2=new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
hanguoji84 2010-01-18
  • 打赏
  • 举报
回复
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
myHttpWebRequest.AllowAutoRedirect = true;
myHttpWebRequest.KeepAlive = true;
myHttpWebRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
myHttpWebRequest.Referer = strReferer;

myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.Method = "POST";

CookieCollection myCookies = null;
CookieContainer myCookieContainer = new CookieContainer();
myHttpWebRequest.CookieContainer = myCookieContainer;

Stream MyRequestStrearm = myHttpWebRequest.GetRequestStream();
StreamWriter MyStreamWriter = new StreamWriter(MyRequestStrearm,System.Text.Encoding.GetEncoding("UTF-8")); //Encoding.ASCII gb2312
//把数据写入HttpWebRequest的Request流
MyStreamWriter.Write(strArgs);
//关闭打开对象
MyStreamWriter.Close();
MyRequestStrearm.Close();

HttpWebResponse response = null;
//System.IO.StreamReader srContent = null;
response = (HttpWebResponse)myHttpWebRequest.GetResponse();

cookieHeader = myHttpWebRequest.CookieContainer.GetCookieHeader(new Uri(strURL));
myCookies = response.Cookies;
hanguoji84 2010-01-18
  • 打赏
  • 举报
回复
保留cookies

string strResult = "";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
myHttpWebRequest.ContentType = "text/html";
myHttpWebRequest.Method = "GET";
myHttpWebRequest.Referer = strReferer;
myHttpWebRequest.Headers.Add("cookie:"+ cookieHeader);

HttpWebResponse response = null;
System.IO.StreamReader sr = null;
response = (HttpWebResponse)myHttpWebRequest.GetResponse();
sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312")); //, Encoding.GetEncoding("gb2312") //utf-8
strResult = sr.ReadToEnd();
return strResult;
tbinjiayou 2009-12-12
  • 打赏
  • 举报
回复
不懂也帮顶个
zixuanzitong 2009-12-12
  • 打赏
  • 举报
回复
budong

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧