C# POST模拟登录发贴

关于POST模拟发贴的例子,网上很多,研究了下HTTP协议,参考了下C#的例子,实现了对于论坛的登录发贴功能

 

本例子对象论坛为无验证码,formhash固定,主要以学习为目的(做完测试一定记得把帖子删掉哦)

 

对http的分析过程就略过了,网上一搜一大堆,我用的Wireshark,HTTPAnalyzer分析, 后者对于post请求的分析更加精确。

 

下面上关键代码 登录

 /// <summary>
        /// post登录
        /// </summary>
        public void SendLogin(string loginUrl, string postData)
        {
            byte[] byteArray = Encoding.GetEncoding("GBK").GetBytes(postData);
            try
            {
                //基于apache服务器,IIS发布的则不需要
                ServicePointManager.Expect100Continue = false;
                CookieContainer cookieContainer = new CookieContainer();
                //创建对url的请求
                httpWebRequest = (HttpWebRequest)WebRequest.Create(loginUrl);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                httpWebRequest.Headers["Accept-Language"] = "zh-cn";
                httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2)";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                //协议方式
                httpWebRequest.Method = "POST";
                //post开始
                //请求内容长度
                httpWebRequest.ContentLength = byteArray.Length;
                Stream dataStream = httpWebRequest.GetRequestStream();
                // 请求数据放入请求流
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();

                //返回html
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("GBK"));
                    //读取响应流
                    string responseFromServer = reader.ReadToEnd();
                    reader.Close();
                    dataStream.Close();
                    httpWebResponse.Close();
                    if (responseFromServer.IndexOf("登录用户名") > 0)
                    {
                        TxtInfo.Text = "登录成功";
                        //保存cookie
                        gCookieCollention = httpWebResponse.Cookies;
                        TxtUid.IsEnabled = false;
                        TxtPwd.IsEnabled = false;
                        LblName.Content = "使用者:" + TxtUid.Text;
                    }
                    else
                    {
                        TxtInfo.Text = responseFromServer;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

  发帖

/// <summary>
        /// post帖子
        /// </summary>
        public void SendInfo(string infoUrl, string postData)
        {
            byte[] byteArray = Encoding.GetEncoding("GBK").GetBytes(postData);
            try
            {
                CookieContainer cookieContainer = new CookieContainer();
                //发出对url的请求
                httpWebRequest = (HttpWebRequest)WebRequest.Create(infoUrl);
                //得到cookie
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.CookieContainer.Add(gCookieCollention);


                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                httpWebRequest.Headers["Accept-Language"] = "zh-cn";
                httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; InfoPath.2)";
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                //协议方式
                httpWebRequest.Method = "POST";
                //与internet建立持久连接
                httpWebRequest.KeepAlive = true;
                //post开始
                //请求内容长度
                httpWebRequest.ContentLength = byteArray.Length;
                Stream dataStream = httpWebRequest.GetRequestStream();
                // 请求数据放入请求流
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();

                //返回html
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                if (httpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding("GBK"));
                    //读取响应流
                    string responseFromServer = reader.ReadToEnd();
                    reader.Close();
                    dataStream.Close();
                    httpWebResponse.Close();

                    if (responseFromServer.IndexOf("帖子发布成功") > 0)
                    {
                        TxtInfo.Text = "发贴成功";
                    }
                    else
                    {
                        TxtInfo.Text = responseFromServer;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

  

posted @ 2012-07-05 16:04  shineme  阅读(661)  评论(1)    收藏  举报