首页 新闻 会员 周边 捐助

能自动登录Baidu后查看登录后的页,但不能发博客,有人能解决不?

0
悬赏园豆:80 [已解决问题] 解决于 2008-12-21 10:53

 CookieContainer cc = new CookieContainer();
            string FormURL = "http://passport.baidu.com/?login";                //处理表单的绝对URL地址
            string FormData = "username=woowater" + "&password=MyPass";    //表单需要提交的参数,注意改为你已注册的信息。
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(FormData);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FormURL);
            request.Method = "POST";    //数据提交方式
            request.ContentType = "application/x-www-form-urlencoded";
            request.KeepAlive = true;
            request.Referer = "http://passport.baidu.com/";
            request.ContentLength = data.Length;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
            //模拟一个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.Default).ReadToEnd();

            string PhotoClassURL = "http://hi.baidu.com/woowater/creat/blog";
            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.Default).ReadToEnd();
            Mystream.Close();
            //sHtml即为你登录之后看到的内容.            
           // MessageBox.Show(sHtml);

                      string Poststr = string.Format("spBlogTitle=yidaxu001&spBlogText=yidaxu001001&spBlogCatName=默认分类&spBlogPower=0&spIsCmtAllow=1&ct=1&cm=1");
            Poststr = EncodePost(Poststr);

            byte[] PostData = encoding.GetBytes(Poststr);
            HttpWebRequest PostRequest = (HttpWebRequest)WebRequest.Create("http://hi.baidu.com/woowater/commit");
            PostRequest.Method = "POST";    //数据提交方式
            PostRequest.ContentType = "application/x-www-form-urlencoded";
            PostRequest.KeepAlive = true;
            PostRequest.Referer = "http://hi.baidu.com/woowater/creat/blog";
            PostRequest.ContentLength = PostData.Length;
            PostRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
            //模拟一个UserAgent
            Stream PostnewStream = PostRequest.GetRequestStream();
                      
            PostnewStream.Write(PostData, 0, PostData.Length);

            PostnewStream.Close();


            PostRequest.CookieContainer = cc;

            HttpWebResponse Postresponse = (HttpWebResponse)PostRequest.GetResponse();
            cc.Add(Postresponse.Cookies);
            Stream PostStream = Postresponse.GetResponseStream();
            string PostWebContent = new StreamReader(PostStream, System.Text.Encoding.Default).ReadToEnd();
            PostStream.Close();
            MessageBox.Show(WebContent);

 

把异常处理代码去掉了

水长的主页 水长 | 初学一级 | 园豆:150
提问于:2008-12-16 14:00
< >
分享
最佳答案
0

似乎还是某个地方模拟的不是很到位,使用httpwatch等工具再仔细看看!关注中...

GUO Xingwang | 老鸟四级 |园豆:3885 | 2008-12-16 14:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册