当前位置:编程学习 > C#/ASP.NET >>

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

想模拟登录开心网,然后直接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没有设置成功,
        }


各位大大看看如何才能登录成功,谢谢! --------------------编程问答-------------------- budong --------------------编程问答-------------------- 不懂也帮顶个 --------------------编程问答-------------------- 保留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;
--------------------编程问答-------------------- 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; --------------------编程问答-------------------- 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();
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,