代码中在发布内容做了一个循环发布,在设置断点调试的时候每一次循环都可以执行输出,但直接运行的时候却只输出了一次结果,不知道是哪方面的原因

解决方案 »

  1.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    using System.Threading;
    namespace 论坛模拟登陆
    {
          public partial class Form1 : Form
          {
                public CookieCollection globaCookie;
                public string Fromhash { set; get; }            public Form1()
                {
                      InitializeComponent();
                }
                private void login(string username, string url, string password)
                {
                      try
                      {
                            log.Text = "【" + DateTime.Now + "】系统正在登录,请您耐心等候......\r\n";
                            url = url + "/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1";
                            string strurl = string.Format("username={0}&password={1}&quickforward=yes&handlekey=ls", username, password);
                            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                            req.Method = "POST";
                            req.Timeout = 5000;
                            req.ContentType = "application/x-www-form-urlencoded";
                            req.CookieContainer  = new CookieContainer();
                            Encoding coding = Encoding.GetEncoding("gb2312");
                            byte[] Date = coding.GetBytes(strurl);
                            req.ContentLength = strurl.Length;
                            Stream reqstr = req.GetRequestStream();
                            reqstr.Write(Date, 0, Date.Length);
                           
                            reqstr.Close();                        //返回数据流
                            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                            Stream datestream = response.GetResponseStream();
                            StreamReader readdate = new StreamReader(datestream, Encoding.GetEncoding("gb2312"));
                            string html = readdate.ReadToEnd();
                            globaCookie = response.Cookies;
                            if (html.IndexOf("登录失败") <0)
                            {
                                  log.Text += "【" + DateTime.Now + "】欢迎您回来" + username + "登录成功......\r\n";
                                  tbshibie.Text = "欢迎您回来";
                                  
                            }
                            else
                            {
                                  tbshibie.Text = "登陆失败";
                                  MessageBox.Show("登录失败请检查用户名和密码是否正确");
                            }                  }
                      catch (Exception ex)
                      {
                            MessageBox.Show(ex.Message);
                      }
                }
                private string  GetHash(string geturl)
                {
                      
                    // string formhash;
                   //   string geturl = tbloginurl.Text;
                      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(geturl);
                      CookieContainer cookies = new CookieContainer();
                      req.CookieContainer = cookies;
                      req.CookieContainer.Add(globaCookie);  
                      req.Method = "GET";                  HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                      Stream reslut = response.GetResponseStream();
                      StreamReader date = new StreamReader(reslut,Encoding.GetEncoding("gb2312"));
                      string html = date.ReadToEnd();
                      if (html.IndexOf("退出")>0)
                      {
                            Fromhash = html.Substring(html.IndexOf("ash=") + 4, 8);
                      }
                      return Fromhash;
                }
                private void send()
                {
                      try
                      {
                            GetHash(tbloginurl.Text);
                          // Thread.SpinWait(1000);
                            string id = textBox2.Text;
                            string url = tbloginurl.Text;
                            string sendurl = string.Format("{0}/forum.php?mod=post&action=newthread&fid={1}&extra=&topicsubmit=yes", url, id);
                            string title = textBox1.Text;
                            string content = richTextBox1.Text;
                            string date = "formhash=" + Fromhash + "&posttime=" + "&wysiwyg=1&subject=" + title + "&message=" + content + "&replycredit_extcredits=0&replycredit_times=1&replycredit_membertimes=1&replycredit_random=100&readperm=&price=&save=&uploadalbum=&newalbum=&usesig=1&allownoticeauthor=1&addfeed=1";
                            byte[] sendDate = Encoding.GetEncoding("gb2312").GetBytes(date);
                            HttpWebRequest sendReq = (HttpWebRequest)WebRequest.Create(sendurl);
                            sendReq.Method = "POST";
                            sendReq.ContentType = "application/x-www-form-urlencoded";
                            sendReq.ContentLength = sendDate.Length;
                            sendReq.CookieContainer = new CookieContainer();
                            sendReq.CookieContainer.Add(globaCookie);
                            Stream sendStream = sendReq.GetRequestStream();
                            sendStream.Write(sendDate, 0, sendDate.Length);
                            sendStream.Close();                        // 获取发布成功信息流
                            HttpWebResponse rep = (HttpWebResponse)sendReq.GetResponse();
                            Stream datestream = rep.GetResponseStream();
                            StreamReader readDate = new StreamReader(datestream, Encoding.GetEncoding("gb2312"));
                            string html = readDate.ReadToEnd();
                            if (html.IndexOf("楼主") > 0)
                            {
                                  log.Text += string.Format("[{0}]【文章】{1}发布成功\r\n", DateTime.Now,title);
                            }
                            readDate.Close();
                            datestream.Close();                  }
                       catch (Exception ex)
                      {                        MessageBox.Show(ex.Message);
                      }
                }
                private void button1_Click(object sender, EventArgs e)
                {
                      
                      if (tbusername.Text.ToString().Trim() !="")
                      {
                            if (tbpassword.Text.ToString().Trim() != "")
                            {
                                  if (tbloginurl.ToString().Trim() != "")
                                  {
                                        string username;
                                        string url;
                                        string password;
                                        username = tbusername.Text;
                                        url = tbloginurl.Text;
                                        password = tbpassword.Text;
                                        login(username, url, password);
                                              for (int i = 0; i <10; i++)
                                              {
                                                    
                                                    send();
                                                                                                                                      }
                                              
                                          
                                  }
                                  else
                                  {
                                        MessageBox.Show("登录网址不能为空");
                                  }
                            }
                            else
                            {
                                  MessageBox.Show("密码不能为空");
                                  return;
                            }
                      }
                      else 
                      {
                            MessageBox.Show("登录帐号不能为空");
                            return;
                      }
                }
          }
    }这是代码
      

  2.   


                                  if (tbloginurl.ToString().Trim() != "")
                                  {
                                        string username;
                                        string url;
                                        string password;
                                        username = tbusername.Text;
                                        url = tbloginurl.Text;
                                        password = tbpassword.Text;
                                        login(username, url, password);
                                              for (int i = 0; i <10; i++)
                                              {
                                                    
                                                    send();
                                                                                                                                      }
                                              
                                          
                                  }
                                  else
                                  {
                                        MessageBox.Show("登录网址不能为空");
                                  }你确定这里的 } 是你少复制了?这个程序能跑起来么?
      

  3.   

    在for循环里执行send后适当延时一下。一般单步调试可执行,去掉断点就不行都是因为多线程或其他原因造成的,可以尝试用延时的方法去解决。
      

  4.   

    send();
    后面加上 Thread.Sleep(5000);
      

  5.   

    send();
    System.Threading.Thread.Sleep(3000);