使用C# HttpWebRequest进行多线程网页提交

使用线程池并发处理request请求及错误重试,使用委托处理UI界面输出。

 http://www.cnblogs.com/Charltsing/p/httpwebrequest.html

                 for (int j = 0; j < _userlist.Count; j++)
                    {
                        user = _userlist[j];
                        if (user.Length == 0) continue;

                        for (int k = 0; k < _passwordlist.Count; k++)
                        {
                            password = _passwordlist[k];
                            if (password.Length == 0) continue;

                            if (threadscount < _MaxThreadsCount)
                            {                                
                                if (_isStop) 
                                {
                                    WriteMessage("\r\n停止创建新线程,正在结束已运行的线程,请等待...\r\n");
                                    goto StopHttp;
                                }
                                HttpWebRequestParameters param = new HttpWebRequestParameters(url, user, password,0,false);
                                ThreadPool.QueueUserWorkItem(new WaitCallback(MakeWebRequest), param);
                                Interlocked.Increment(ref threadscount);
                                Interlocked.Increment(ref requestcount);
                            }
                            else
                            {
                                k--;
                            }
                            WriteStatus("已使用线程数:" + threadscount.ToString() +
                                        " 已发送请求数:" + requestcount.ToString() +
                                        " 已完成请求数:" + responsecount.ToString());
                        }
                        System.GC.Collect();
                    }

  

 

联系QQ:564955427

欢迎交流!

posted on 2015-09-07 23:36 Charltsing 阅读(...) 评论(...) 编辑 收藏

导航