当前位置 博文首页 > 文章内容

    http请求入队列以及遇到的问题总结:ezreal_pan的博客

    作者:shunshunshun18 栏目:未分类 时间:2021-10-17 20:32:33

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    推荐 IIS7批量FTP管理工具 IIS7批量FTP管理
    IIS7批量FTP管理功能说明:
    1、可批量导入,导出FTP信息
    2、其他ftp工具有的功能,我们也有
    3、特色功能:可以定时上传下载
    4、数据信息列表化、一眼就能知道那个是那个
    5、批量连接 标签页式切换 方便快捷
    6、7大连接模式 更多好的兼容
    7、内嵌编辑器 有效解决普通txt记事本乱码
    8、锁屏功能 当程序有规定时间内没人操作,则自动锁程序。输入密码才可以正常操作

    本产品适用于:懒得记录FTP信息和有批量定时备份,上传下载的运维或站长。

    图:


    IIS7批量FTP管理

                                                           http请求入队列排队执行以及遇到的问题总结

    队列以及加锁的注意事项,如下所示,记录之,线程的知识体系尚需完善。

    public class HttpContext
        {
            public HttpListenerContext httpListenerContext { get; set; }
            public Hashtable para { get; set; }
        }

     

     public partial class Form1 : Form
        {
            private HttpListener m_verifyhttplisten;
            private object m_listenlocker = new object();
            private ConcurrentQueue<HttpContext> m_httpconQueue = new ConcurrentQueue<HttpContext>();
            HttpContext m_httpContext = new HttpContext();
            HttpListenerContext httpListener;
            object locker = new object();
            static int num = 1;
            public Form1()
            {
                InitializeComponent();
                m_verifyhttplisten = new HttpListener();
                m_verifyhttplisten.Prefixes.Add("http://192.168.1.199:50000/verify/");
                m_verifyhttplisten.Start();
                ThreadPool.QueueUserWorkItem(new WaitCallback(obj =>
                    {
                        httpRequestHandle2();
                    }));
                Thread thread2 = new Thread(ceshi);
                thread2.Start();
            }
            private void httpRequestHandle2()
            {
                while (true)
                {
                    try
                    {
                        //等待请求连接,没有请求则GetContext处于阻塞状态
                        HttpListenerContext requestContext = m_verifyhttplisten.GetContext();
                        ThreadPool.QueueUserWorkItem(new WaitCallback(verifyhttplistern), requestContext);
                    }
                    catch (Exception ex)
                    {
                        //LogHelperBLL.CreateErrorLogTxt("httpRequestHandle2", ex.Source, ex.Message);
                    }
                }
            }
            private void verifyhttplistern(object requestcontext)
            {
                try
                {
                    lock (m_listenlocker)
                    {
                        //获取请求参数                    
                        Hashtable para = new Hashtable();
                        //返回值                     
                        Hashtable rtn = new Hashtable();
                        //客户端发送过来的信息
                        HttpListenerContext request = (HttpListenerContext)requestcontext;
    
                        //获取Post请求中的参数和值帮助类
                        HttpListenerHelper httppost = new HttpListenerHelper(request);
    
                        //获取Post过来的参数和数据  
                        List<HttpListenerPostValue> lst = httppost.GetHttpListenerPostValue();
                        try
                        {
                           
                                    HttpContext httpContext = new HttpContext(); 
                                    httpContext.httpListenerContext = request;
                                    httpContext.para = para;
                                    try
                                    {
                                        m_httpconQueue.Enqueue(httpContext);
                                    }
                                    catch (Exception ex)
                                    {
                                       
                                    }
                            }
                                   
                        }
                        catch (Exception ex)
                        {
                           
                        }
                    }
                }
                catch (Exception ex)
                {
                    
                }
            }
            private void ceshi()
            {
                while (true)
                {
                    try
                    {
                        Hashtable para = new Hashtable();
                        Hashtable rtn = new Hashtable();
                        lock (locker)
                        {
                            HttpContext httpContext = new HttpContext();
                            if (m_httpconQueue.Count > 0)
                            {
    
                                m_httpconQueue.TryDequeue(out httpContext);
                            }
                            else if (m_httpconQueue.Count == 0)
                            {
                                continue;
                            }
                            HttpListenerContext request = httpContext.httpListenerContext;
                            para = httpContext.para;
                            try
                            {
                                rtn.Add("status", 200);
                                rtn.Add("code", 1);
                                rtn.Add("message", "ceshi");
                                //Response  
                                request.Response.StatusCode = 200;
                                request.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                                request.Response.ContentType = "application/json";
                                request.Response.ContentEncoding = Encoding.UTF8;
                                byte[] buffer = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(rtn));
                                request.Response.ContentLength64 = buffer.Length;
                                var output = request.Response.OutputStream;
                                output.Write(buffer, 0, buffer.Length);
                                output.Close();
                            }
                            catch (System.Net.WebException ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                        }
                        Thread.Sleep(100);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
    
        }
        

     上面的程序段时不时的会报“提交响应后无法执行此操作 ”这个错误。我就一直被这个错误困扰误导了,一直沿着这个方向找问题。后来发现我找问题的方向是不对的。而是应该是线程安全方面的问题。队列入队的对象加锁出现问题,导致出队列执行响应的时候报这个异常。即:入队的时候的这个对象应该是局部变量,加在锁内。

    如上图所示:入队的这个对象是全局的对象,就会导致异常。

    cs