zoukankan      html  css  js  c++  java
  • 使用C# HttpWebRequest进行多线程网页提交。Async httpclient/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("
    停止创建新线程,正在结束已运行的线程,请等待...
    ");
                                        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();
                        }
    

      

    ***************************************

    更新说明:

    上面的方法使用线程池,这种方法没有task方便。

    我在2016年11月用async/await+httpclient 重新写了一个版本,实现了完整的任务发布、异步提交http请求、超时或者手工取消的操作

    此外,还写了一个使用async+HttpWebRequest+task实现的异步提交任务及超时处理的代码版本,这种方法不需要自己实现多线程,异步操作本身就是通过线程池来实现多线程的。

     

    联系QQ:564955427

    欢迎交流!

  • 相关阅读:
    编译器内置宏实现调试信息输出
    走进C标准库(4)——"stdio.h"中的putc
    走进C标准库(5)——"stdio.h"中的其他部分函数
    走进C标准库(2)——"stdio.h"中的fopen函数
    [转]深度探索C语言函数可变长参数
    C语言I博客作业02
    C语言I博客作业02
    第一周c语音作业
    什么是模块化,模块化的好处又是什么?
    服务端渲染和客户端渲染
  • 原文地址:https://www.cnblogs.com/Charltsing/p/httpwebrequest.html
Copyright © 2011-2022 走看看