2 a379101 a379101 于 2013.06.09 14:09 提问

C# 如何使用 HttpWebReques 编写baidu(百度)自动登录,请高手帮忙看看,如何解决?

下面是我写的代码,不知道那里出错,实现不了登录,请帮忙解决...先说声谢谢啦代码:


string postdata = "ppui_logintime=482599&charset=utf-8&codestring=&token=e80e24119525675ba6ee9e0e15c6a8be&isPhone=false&index=0&u=&safeflg=0&staticpage=http%3A%2F%2Fwww.baidu.com%2Fcache%2Fuser%2Fhtml%2Fjump.html&loginType=1&tpl=mn&callback=parent.bdPass.api.login._postCallback&username=xx&password=xx&verifycode=&mem_pass=on";
            string url = "https://passport.baidu.com/v2/api/?login";
            //准备发送请求
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            Encoding myEncoding = Encoding.GetEncoding("UTF-8");
            byte[] data = myEncoding.GetBytes(postdata);
            CookieContainer cc=new CookieContainer();

            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            request.KeepAlive = true;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E))";
            request.CookieContainer=cc;          
            request.ContentLength = data.Length;

            //





            //发送数据

            Stream webStream = request.GetRequestStream();
            webStream.Write(data, 0, data.Length);
            //webStream.Close();

            webStream.Flush();

            //获取返回数据

            HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();

            StreamReader reader = new StreamReader(webResponse.GetResponseStream(), myEncoding );
            postdata = reader.ReadToEnd();
            Console.WriteLine(postdata);
            webResponse.Close();
            postdata.Trim();


            //Cookies 

            CookieContainer cs = new System.Net.CookieContainer();
            System.Net.CookieCollection cc1 = cc.GetCookies(new Uri("https://passport.baidu.com/v2/api/?login")); 
            foreach (System.Net.Cookie item in cc1) { 
                Console.WriteLine(item.Name); 
                Console.WriteLine(item.Value); }


            webBrowser1.Navigate("www.baidu.com");
Csdn user default icon
上传中...
上传图片
插入图片