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

C#模拟自动登陆一个网站,但是这个老是出错

C#模拟自动登陆一个网站,但是这个老是出错。我试过登陆163邮箱、http://school.2embed.com这两个网站,都可以实现,但是登陆http://profile.2embed.com/index.php?option=com_user&view=login&return=aHR0cDovL3Byb2ZpbGUuMmVtYmVkLmNvbS8=这个网站的时候老是出错,请问是怎么回事?
private void button1_Click(object sender, EventArgs e)
        {
            CookieContainer cc = new CookieContainer();
            //url和postData的表单变量用httpwatch查看
            //string url = "http://school.2embed.com/login/index.php";
            string url = "http://profile.2embed.com/index.php?option=com_user&view=login&return=aHR0cDovL3Byb2ZpbGUuMmVtYmVkLmNvbS8=";
            //string url = "https://reg.163.com/logins.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ntesdoor?hid%3D10010102%26lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D-1";
            //string postData = "user=" + this.textBox1.Text + "&username=" + this.textBox1.Text + "@126.com&password=" + this.textBox2.Text;
            string postData = "username=zbb&passwd=541469635&Submit=%E7%99%BB%E5%BD%95&option=com_user&task=login&return=aHR0cDovL3Byb2ZpbGUuMmVtYmVkLmNvbS8%3D&0ca9423a6398a0d485144f59e6fb9846=1";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化

            HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri(url));
            webRequest2.CookieContainer = cc;
            webRequest2.Method = "POST";
            webRequest2.ContentType = "application/x-www-form-urlencoded";
            webRequest2.ContentLength = byteArray.Length;
            webRequest2.Date=DateTime.Now;
            
            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.UTF8);
            
            string text = sr2.ReadToEnd();
            this.richTextBox1.Text = text;
        } C# 登录 URL --------------------编程问答-------------------- 我也遇到了类似问题,正期待解决http://bbs.csdn.net/topics/390476920

我感觉你的问题是
1.用IE的网络监控看一下确切的post url地址多少?
2.post页面可能要识别你之前的页面地址 webRequest2.refere 以及前以页面的页面会话值。

期待你能解决。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,