首页 新闻 搜索 专区 学院

请帮我看看代码,模拟登陆,发帖,可以登陆了,发帖时不能携带cookie

0
悬赏园豆:100 [已解决问题] 解决于 2012-07-06 01:48
CookieContainer cookieContainer = null;
        //登陆
        public string Login(string posturl, string postData)
        {
            Stream outstream = null;
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            Encoding encoding = System.Text.Encoding.GetEncoding("utf-8");
            byte[] data = encoding.GetBytes(postData);
            // 准备请求...
            try
            {
                // 设置参数
                request = WebRequest.Create(posturl) as HttpWebRequest;
               
                request.CookieContainer = cookieContainer;

                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
                request.ContentLength = data.Length;
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Close();
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                foreach (Cookie item in response.Cookies)
                {
                    cookieContainer.Add(new Uri(posturl), item);
                }
 
                //返回结果网页(html)代码
                string content = sr.ReadToEnd();
                string err = string.Empty;
                return content;
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                return string.Empty;
            }
        }
        //发帖
        public void  fate()
        {  
            //发帖
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://localhost/upload/forum.php?mod=post&action=newthread&fid=2&extra=&topicsubmit=yes"));
            request.Method = "POST";
            request.ContentType = "text/html; charset=gb2312";
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.CookieContainer = cookieContainer;
 
            request.CookieContainer.SetCookies(new System.Uri("http://localhost/upload/forum.php?mod=post&action=newthread&fid=2&extra=&topicsubmit=yes"), "Set-Cookie");
            byte[] data = Encoding.GetEncoding("utf-8").GetBytes("formhash=d0674f9a&posttime=1299773427&wysiwyg=1&subject=22222222222wwwwwwwwww222222222222222222222&message=1111111111111111111111111111111111ssssssssssssss1111111111111111111111111111111111ssssssssssssss1111111111111111111111111111111111ssssssssssssss1111111111111111111111111111111111ssssssssssssss1111111111111111111111111111111111ssssssssssssss1111111111111111111111111111111111ssssssssss%0D%0A&save=&uploadalbum=&newalbum=&readperm=&price=&usesig=1&allownoticeauthor=1");
            request.CookieContainer = cookieContainer;
            Stream request_Stream = request.GetRequestStream();
            request_Stream.Write(data,0,data.Length);
            request_Stream.Close();


            HttpWebResponse response3 = (HttpWebResponse)request.GetResponse();
            Stream sssssss = response3.GetResponseStream();
            StreamReader sbb = new StreamReader(sssssss,  System.Text.Encoding.GetEncoding("utf-8"));
            string bb=sbb.ReadToEnd();
              
        }


 private void button2_Click(object sender, EventArgs e)
        {  cookieContainer=new CookieContainer();
           string con = Login("http://localhost/upload/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&inajax=1", "fastloginfield=username&username=admin&password=admin&quickforward=yes&handlekey=ls&questionid=0&answer=");

            fate();

                }

请帮我看看代码,模拟登陆,发帖,可以登陆了,发帖时不能携带cookie

这是转发别人的一个问题,因为自己也不懂,那个帖子也没给什么结果

初学C# 百思不得其解

c#
shineme的主页 shineme | 初学一级 | 园豆:29
提问于:2012-07-05 21:18
< >
分享
最佳答案
0

request.CookieContainer = cookieContainer,只要这一句就好了,.net 会帮你自动管理cookie 不用你自己SetCookies和Add操作

收获园豆:100
garry | 菜鸟二级 |园豆:495 | 2012-07-05 23:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册