示例#1
0
        /*
         * public static string CommonHttpRequest(string data, string uri, string type)
         * {
         *
         *  //Web访问对象,构造请求的url地址
         *  string serviceUrl = uri;
         *
         *  //构造http请求的对象
         *  HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
         *  myRequest.Timeout = 10000;
         *  //转成网络流
         *  byte[] buf = ENCODING.GetBytes(data);
         *  //设置
         *  myRequest.Method = type;
         *  myRequest.ContentLength = buf.LongLength;
         *  myRequest.ContentType = "application/json";
         *
         *  //将客户端IP加到头文件中
         *
         *   //string sRealIp = GetHostAddress();
         *  //if (!string.IsNullOrEmpty(sRealIp))
         *  //{
         *   //   myRequest.Headers.Add("ClientIp", sRealIp);
         *  //}
         *
         *
         *
         *  using (Stream reqstream = myRequest.GetRequestStream())
         *  {
         *      reqstream.Write(buf, 0, (int)buf.Length);
         *  }
         *  HttpWebResponse resp = myRequest.GetResponse() as HttpWebResponse;
         *  System.IO.StreamReader reader = new System.IO.StreamReader(resp.GetResponseStream(), ENCODING);
         *  string result = reader.ReadToEnd();
         *  reader.Close();
         *  resp.Close();
         *  return result;
         * }
         */

        public static string HTTPJsonGet(QuestionaireEntity entity)
        {
            string url    = getURL(entity);
            string result = string.Empty;

            try
            {
                //测试时候使用的接口
                //url = "http://192.168.0.102:8000/hello/sleep";
                System.Diagnostics.Debug.WriteLine("开始调用接口: " + url);
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.ContentType      = "application/json";
                request.Method           = "GET";
                request.ReadWriteTimeout = 1000;
                request.Timeout          = 2000;
                HttpWebResponse        resp   = request.GetResponse() as HttpWebResponse;
                System.IO.StreamReader reader = new System.IO.StreamReader(resp.GetResponseStream(), ENCODING);
                result = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                logForm log = logForm.InstanceLogForm();
                log.addLog(CreateController.getLogStr(entity, "  刷题入库失败! 刷题结果::" + ex.Message));
                System.Diagnostics.Debug.WriteLine("HTTPJsonGet调用超时或者异常:" + ex.Message);
            }


            //string result = "{\"code\":200,\"msg\":\"问卷添加成功,感谢您的参与!\",\"data\":[]}";
            return(result);
        }
        public static void slotTimeChange(object sender, EventArgs e)
        {
            //刷题间隔时间改变事件
            NumericUpDown slotNum = (NumericUpDown)sender;

            try
            {
                int value = int.Parse(slotNum.Value.ToString());
            }
            catch (Exception)
            {
                MessageBox.Show("请输入有效数字!");
            }
            //加上锁 进行对QuestionaireData.result; 的修改
            lock (syncLocker)
            {
                logForm log = logForm.InstanceLogForm();
                List <QuestionaireEntity> questionList = QuestionaireData.result;
                QuestionaireEntity        entity       = new QuestionaireEntity();
                foreach (var q in questionList)
                {
                    if ("slotNum_" + q.SeriaNum == slotNum.Name)
                    {
                        QuestionaireData.result.Remove(q);
                        entity = q;
                        break;
                    }
                }
                entity.SlotTime = int.Parse(slotNum.Value.ToString());
                QuestionaireData.result.Add(entity);
                log.addLog("序号: " + entity.Code + "修改了刷题间隔为: " + entity.SlotTime);
            }
            System.Diagnostics.Debug.WriteLine("刷题间隔时间修改为: " + slotNum.Value);
        }
示例#3
0
        private static string getURL(QuestionaireEntity entity)
        {
            string result         = "";
            string csrf_test_name = "bb13c888c7da784789d130c081ca9d53";
            string url            = entity.Address;

            url = url.Replace("m=index", "m=addSelOptions");
            string optioned = CreateAnswer.createAnswer(entity);

            result = url + "&optioned=" + optioned + "&csrf_test_name=" + csrf_test_name;
            return(result);
        }
示例#4
0
        public static string createAnswer(QuestionaireEntity entity)
        {
            string result = "";
            Random rd     = new Random();
            Dictionary <int, int> answer = entity.AnswerScope;

            foreach (var item in answer)
            {
                int randomValue = rd.Next(1, item.Value + 1);
                result += randomValue.ToString() + ",";
            }
            System.Diagnostics.Debug.WriteLine("结果: " + result);
            return(result);
        }
        /**
         * 设置打印的日志内容
         */
        public static string getLogStr(QuestionaireEntity entity, string msg)
        {
            string name = entity.Name;

            if (entity.Name.Length < 20)
            {
                for (int i = 0; i < (20 - entity.Name.Length); i++)
                {
                    name += "  ";
                }
            }
            string resultStr = "";

            resultStr = "序号: " + entity.Code + " 问卷名称: " + name + msg;
            return(resultStr);
        }
        //生成对应的空间
        public static void addController(int x, int y, Form1 form, QuestionaireEntity entity)
        {
            //主窗口赋值给变量
            mainForm = form;
            //序号控件
            Label seriaLable = new Label();

            seriaLable.Text     = entity.KindNum;
            seriaLable.Size     = new System.Drawing.Size(30, 15);
            seriaLable.Location = new System.Drawing.Point(10, y);
            form.Controls.Add(seriaLable);

            Label namelabel = new Label();

            namelabel.Text     = entity.Name;
            namelabel.Size     = new System.Drawing.Size(140, 30);
            namelabel.Location = new System.Drawing.Point(55, y);
            form.Controls.Add(namelabel);

            Label numlabel = new Label();

            numlabel.Text     = entity.QuestionNum.ToString();
            numlabel.Size     = new System.Drawing.Size(95, 15);
            numlabel.Location = new System.Drawing.Point(200, y);
            form.Controls.Add(numlabel);

            Label codeLabel = new Label();

            codeLabel.Text     = entity.Code;
            codeLabel.Size     = new System.Drawing.Size(50, 30);
            codeLabel.Location = new System.Drawing.Point(300, y);
            form.Controls.Add(codeLabel);

            NumericUpDown upNumText = new NumericUpDown();

            upNumText.Maximum  = 10000;
            upNumText.Name     = "upNum_" + entity.SeriaNum;
            upNumText.Text     = entity.UpNum.ToString();
            upNumText.Size     = new System.Drawing.Size(50, 15);
            upNumText.Location = new System.Drawing.Point(370, y);
            form.Controls.Add(upNumText);
            //添加内容修改事件
            upNumText.ValueChanged += new System.EventHandler(upNumChange);


            NumericUpDown slotNum = new NumericUpDown();

            slotNum.Maximum  = 10000;
            slotNum.Name     = "slotNum_" + entity.SeriaNum;
            slotNum.Value    = entity.SlotTime;;
            slotNum.Size     = new System.Drawing.Size(50, 25);
            slotNum.Location = new System.Drawing.Point(440, y);
            form.Controls.Add(slotNum);
            //添加内容修改事件
            slotNum.ValueChanged += new System.EventHandler(slotTimeChange);

            Button actionButton = new Button();

            actionButton.Name     = entity.SeriaNum;
            actionButton.Text     = "单条执行";
            actionButton.Location = new System.Drawing.Point(560, y);
            form.Controls.Add(actionButton);
            //button按钮添加事件
            actionButton.Click += new System.EventHandler(CreateController.btn_Click);

            Label successNumLabel = new Label();

            successNumLabel.Text = "完成数量: 0";
            //设置label名称,以便后续查询到
            successNumLabel.Name     = "label_" + entity.SeriaNum;
            successNumLabel.AutoSize = true;
            successNumLabel.Location = new System.Drawing.Point(670, y);
            form.Controls.Add(successNumLabel);
        }
        public static void exercise(Form1 mainForm, QuestionaireEntity questEntity)
        {
            QuestionaireEntity entity = questEntity;
            logForm            log    = logForm.InstanceLogForm();

            log.addLog(getLogStr(entity, " 开始刷题"));

            int tempNum = 0;

            successNum.TryGetValue(entity.SeriaNum, out tempNum);

            while (tempNum < entity.UpNum && !entity.CloseThread)
            {
                System.Diagnostics.Debug.WriteLine("开始调用接口,调用时间间隔:" + (entity.SlotTime * 1000).ToString());

                // 进行接口调用,判断是否调用成功,更新主界面数据,调用记录保存
                String resultStr = HttpUtil.HTTPJsonGet(entity);
                System.Diagnostics.Debug.WriteLine("接口返回: " + resultStr);

                //解析返回的结果
                JavaScriptObject re = JavaScriptConvert.DeserializeObject <JavaScriptObject>(resultStr);
                if (re == null)
                {
                    //log.addLog(getLogStr(entity, " 刷题入库失败! 刷题结果: " + resultStr));
                    Thread.Sleep(entity.SlotTime * 1000);
                    continue;
                }
                string msg = re["msg"].ToString();
                System.Diagnostics.Debug.WriteLine("code: " + re["code"].ToString());
                System.Diagnostics.Debug.WriteLine("msg: " + msg);
                if ("200".Equals(re["code"].ToString()))
                {
                    if (msg.Contains("问卷添加成功"))
                    {
                        //添加成功了
                        int num = 0;
                        if (successNum.ContainsKey(entity.SeriaNum))
                        {
                            //有这个线程的完成刷题的数量值
                            successNum.TryGetValue(entity.SeriaNum, out num);
                        }
                        else
                        {
                            //没有这个线程的完成刷题的数量值
                            successNum.Add(entity.SeriaNum, 0);
                        }
                        num++;
                        //新值加入到集合中
                        successNum.Remove(entity.SeriaNum);
                        successNum.Add(entity.SeriaNum, num);
                        //修改对应label的值
                        foreach (var control in mainForm.Controls)
                        {
                            if (control.GetType().Name.Equals("Label"))
                            {
                                Label  lb        = (Label)control;
                                string labelName = "label_" + entity.SeriaNum;
                                if (labelName.Equals(lb.Name))
                                {
                                    //找到了对应的label,开始修改值
                                    lb.Text = "完成数量: " + num;
                                    tempNum = num;
                                }
                            }
                        }
                        //添加日志
                        log.addLog(getLogStr(entity, " 刷题入库成功! 刷题结果: " + msg));
                    }
                    else
                    {
                        //已经填写过了问卷或者其他情况
                        log.addLog(getLogStr(entity, " 刷题入库失败! 刷题结果: " + msg));
                    }
                }
                List <QuestionaireEntity> questionList = QuestionaireData.result;
                foreach (var q in questionList)
                {
                    if (q.SeriaNum == entity.SeriaNum)
                    {
                        entity = q;
                        break;
                    }
                }
                //需要暂停20秒左右的时间,等待ip更新
                Thread.Sleep(entity.SlotTime * 1000);
            }


            log.addLog(getLogStr(entity, " 刷题结束 "));
            //走到这里,刷单线程,即将结束
            //修改按钮的显示
            foreach (var control in mainForm.Controls)
            {
                if (control.GetType().Name.Equals("Button"))
                {
                    Button tmpBtn = (Button)control;
                    if (entity.SeriaNum.Equals(tmpBtn.Name))
                    {
                        //找到了触发此线程的控件,在线程完成任务后,修改按钮状态
                        tmpBtn.Text    = "单条执行";
                        tmpBtn.Enabled = true;
                        //先维护好线程集合
                        threadDict.Remove(entity.SeriaNum);
                    }
                }
            }
        }
        public static void btn_Click(object sender, EventArgs e)
        {
            logForm log = logForm.InstanceLogForm();
            Button  btn = (Button)sender;
            //获取需要的刷题信息
            List <QuestionaireEntity> questionList = QuestionaireData.result;
            QuestionaireEntity        entity       = new QuestionaireEntity();

            foreach (var q in questionList)
            {
                if (q.SeriaNum == btn.Name)
                {
                    entity = q;
                    break;
                }
            }

            if (btn.Text == "单条执行")
            {
                entity.CloseThread = false;
                //执行线程,开始刷题
                if (threadDict.ContainsKey(btn.Name))
                {
                    //已经有这么一个线程了
                    MessageBox.Show("此序号的刷题线程已经启动,请勿重复启动!");
                    return;
                }

                //新建刷题线程
                Thread thread = new Thread(() => exercise(mainForm, entity));
                //加入到线程集合中
                threadDict.Add(btn.Name, thread);
                thread.Name = btn.Name;
                btn.Text    = "停止";
                //线程要最后启动
                thread.Start();
            }
            else
            {
                //执行线程,结束刷题
                btn.Text    = "正在结束...";
                btn.Enabled = false;

                /**
                 * Thread th;
                 * threadDict.TryGetValue(btn.Name, out th);
                 * //线程集合中移除此线程
                 *
                 * if (th != null)
                 * {
                 *  //停止此线程
                 *  log.addLog(getLogStr(entity, " 手动暂停了此刷题"));
                 *  th.Abort();
                 *  while (th.ThreadState != ThreadState.Aborted)
                 *  {
                 *      //当调用Abort方法后,如果thread线程的状态不为Aborted,主线程就一直在这里做循环,直到thread线程的状态变为Aborted为止
                 *      Thread.Sleep(100);
                 *  }
                 *  threadDict.Remove(btn.Name);
                 * }
                 **/
                entity.CloseThread = true;
            }
        }