POST phpwind 登陆

private void button2_Click(object sender, EventArgs e)
      {
          string FormURL = "http://bbs.mydigit.cn/login.php";
          string list2 = "?foward=&jumpurl=http://bbs.mydigit.cn&step=2&lgt=0&pwuser=hdl&pwpwd=hdl&question=0&customquest=&answer=&hideid=1&cktime=31536000";

          System.Net.CookieContainer cook = new System.Net.CookieContainer();
          string html = Login(FormURL, list2, out cook);
          FormURL = "http://bbs.mydigit.cn/jobcenter.php?action=punch&verify=ce2c8f43&nowtime=2309477852&verify=ce2c8f43&step=2";
          html = HttpRequestHelp.getPage(FormURL, "", ref cook);
          html = Login(FormURL, list2, out cook);

          //这里的cookies,需要从哪里获得????
          //问题补充  2009-07-20 18:37
          //request.Headers.Add("Accept-Encoding", "gzip, deflate");

          //没事了。去掉这句,就不乱码了。

      }

      private static string Login(string FormURL, string FormData, out CookieContainer container)
      {
          Encoding encoding = new ASCIIEncoding();
          byte[] data = encoding.GetBytes(FormData);

          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FormURL);
          request.Method = "POST";
          request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
          request.Referer = FormURL;
          request.Headers.Add("Accept-Language", "zh-cn");
          request.ContentType = "application/x-www-form-urlencoded";
          request.Headers.Add("UA-CPU", "x86");
          request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
          request.ContentLength = data.Length;

          //应该主要就是cookies的问题,下面方法无效。
          if (container == null || container.Count == 0)
          {
              container = new CookieContainer();
              container.GetCookies(new Uri(FormURL));
          }
          request.CookieContainer = container;
          Stream newStream = request.GetRequestStream();
          System.Type type = newStream.GetType();
          newStream.Write(data, 0, data.Length);
          newStream.Close();

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

          Stream responsestr = response.GetResponseStream();
          StreamReader responsesr = new StreamReader(responsestr, Encoding.GetEncoding("GB2312"));

          //获得测试页面无效,内容为不知编码格式的数据,不是GB2312、UTF8、Unicode
          string html = responsesr.ReadToEnd();
          responsestr.Close();
          responsesr.Close();
          return html;
      }

posted @ 2011-07-01 23:52  梦想(胡大利)  阅读(473)  评论(1编辑  收藏  举报