zoukankan      html  css  js  c++  java
  • HttpNameValueCollection处理GET POST提交数据类

    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Web;
    
    namespace HttpServerDemo
    {
        public class HttpNameValueCollection
        {
            public class File
            {
                private string _fileName;
                public string FileName { get { return _fileName ?? (_fileName = ""); } set { _fileName = value; } }
    
                private string _fileData;
                public string FileData { get { return _fileData ?? (_fileName = ""); } set { _fileData = value; } }
    
                private string _contentType;
                public string ContentType { get { return _contentType ?? (_contentType = ""); } set { _contentType = value; } }
            }
    
            private NameValueCollection _get;
            private Dictionary<string, File> _files;
            private readonly HttpListenerContext _ctx;
    
            public NameValueCollection Get { get { return _get ?? (_get = new NameValueCollection()); } set { _get = value; } }
            public NameValueCollection Post { get { return _ctx.Request.QueryString; } }
            public Dictionary<string, File> Files { get { return _files ?? (_files = new Dictionary<string, File>()); } set { _files = value; } }
    
            private void PopulatePostMultiPart(string post_string)
            {
                var boundary_index = _ctx.Request.ContentType.IndexOf("boundary=") + 9;
                var boundary = _ctx.Request.ContentType.Substring(boundary_index, _ctx.Request.ContentType.Length - boundary_index);
    
                var upper_bound = post_string.Length - 4;
    
                if (post_string.Substring(2, boundary.Length) != boundary)
                    throw (new InvalidDataException());
    
                var raw_post_strings = new List<string>();
                var current_string = new StringBuilder();
    
                for (var x = 4 + boundary.Length; x < upper_bound; ++x)
                {
                    if (post_string.Substring(x, boundary.Length) == boundary)
                    {
                        x += boundary.Length + 1;
                        raw_post_strings.Add(current_string.ToString().Remove(current_string.Length - 3, 3));
                        current_string = null;
                        continue;
                    }
    
                    current_string.Append(post_string[x]);
    
                    var post_variable_string = current_string.ToString();
    
                    var end_of_header = post_variable_string.IndexOf("\r\n\r\n");
    
                    if (end_of_header == -1) throw (new InvalidDataException());
    
                    var filename_index = post_variable_string.IndexOf("filename=\"", 0, end_of_header);
                    var filename_starts = filename_index + 10;
                    var content_type_starts = post_variable_string.IndexOf("Content-Type: ", 0, end_of_header) + 14;
                    var name_starts = post_variable_string.IndexOf("name=\"") + 6;
                    var data_starts = end_of_header + 4;
    
                    if (filename_index == -1) continue;
    
                    var filename = post_variable_string.Substring(filename_starts, post_variable_string.IndexOf("\"", filename_starts) - filename_starts);
                    var content_type = post_variable_string.Substring(content_type_starts, post_variable_string.IndexOf("\r\n", content_type_starts) - content_type_starts);
                    var file_data = post_variable_string.Substring(data_starts, post_variable_string.Length - data_starts);
                    var name = post_variable_string.Substring(name_starts, post_variable_string.IndexOf("\"", name_starts) - name_starts);
                    Files.Add(name, new File() { FileName = filename, ContentType = content_type, FileData = file_data });
                    continue;
    
                }
            }
    
            private void PopulatePost()
            {
                if (_ctx.Request.HttpMethod != "POST" || _ctx.Request.ContentType == null) return;
    
                var post_string = new StreamReader(_ctx.Request.InputStream, _ctx.Request.ContentEncoding).ReadToEnd();
    
                if (_ctx.Request.ContentType.StartsWith("multipart/form-data"))
                    PopulatePostMultiPart(post_string);
                else
                    Get = HttpUtility.ParseQueryString(post_string);
    
            }
    
            public HttpNameValueCollection(ref HttpListenerContext ctx)
            {
                _ctx = ctx;
                PopulatePost();
            }
    
    
        }
    }
  • 相关阅读:
    VMWare磁盘配置的问题终于解决了!!
    十种老板不可追随
    关于ASP无组件上传在2003下出错
    设计模式的有趣解释-追MM[转]
    "未能在给定的程序集中找到任何适合于指定的区域性(或非特定区域性)的资源"解决办法
    今天看到了DNN3.0.4,感觉挺不错的,确实有很大的改进!!!
    今天加入了博客园
    一个女孩写的经典程序!!! (转载)
    加了强名后经常出现错误“程序集清单定义与程序集引用不匹配”
    C#写一个URL编码转换GB23121的方法,然后可以取到天气预报
  • 原文地址:https://www.cnblogs.com/skyblue/p/2385430.html
Copyright © 2011-2022 走看看