• 使用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

    欢迎交流!

  • 相关阅读:
    Java编程思想学习(三)----第三章:操作符
    mybatis入门
    responsebody和requestbody的使用
    一个Interface 继承多个Interface 的总结
    requirejs中Shims使用说明
    java 中解析json步骤
    @transient加在属性前的作用
    实现serializable接口的作用
    springmvc源码解析-初始化
    @RequestMapping注解详解
  • 原文地址:https://www.cnblogs.com/Charltsing/p/httpwebrequest.html
Copyright © 2020-2023  润新知