架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 24885|回复: 0

[资料] HttpListenner监听Post请求参数值实体

[复制链接]
发表于 2016-7-12 14:10:25 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;

  8. namespace XXXX
  9. {
  10.     /// <summary>
  11.     /// HttpListenner监听Post请求参数值实体
  12.     /// </summary>
  13.     public class HttpListenerPostValue
  14.     {
  15.         /// <summary>
  16.         /// 0=> 参数
  17.         /// 1=> 文件
  18.         /// </summary>
  19.         public int type = 0;
  20.         public string name;
  21.         public byte[] datas;
  22.     }

  23.     /// <summary>
  24.     /// 获取Post请求中的参数和值帮助类
  25.     /// </summary>
  26.     public class HttpListenerPostParaHelper
  27.     {
  28.         private HttpListenerContext request;

  29.         public HttpListenerPostParaHelper(HttpListenerContext request)
  30.         {
  31.             this.request = request;
  32.         }

  33.         private bool CompareBytes(byte[] source, byte[] comparison)
  34.         {
  35.             try
  36.             {
  37.                 int count = source.Length;
  38.                 if (source.Length != comparison.Length)
  39.                     return false;
  40.                 for (int i = 0; i < count; i++)
  41.                     if (source[i] != comparison[i])
  42.                         return false;
  43.                 return true;
  44.             }
  45.             catch
  46.             {
  47.                 return false;
  48.             }
  49.         }

  50.         private byte[] ReadLineAsBytes(Stream SourceStream)
  51.         {
  52.             var resultStream = new MemoryStream();
  53.             while (true)
  54.             {
  55.                 int data = SourceStream.ReadByte();
  56.                 resultStream.WriteByte((byte)data);
  57.                 if (data == 10)
  58.                     break;
  59.             }
  60.             resultStream.Position = 0;
  61.             byte[] dataBytes = new byte[resultStream.Length];
  62.             resultStream.Read(dataBytes, 0, dataBytes.Length);
  63.             return dataBytes;
  64.         }

  65.         /// <summary>
  66.         /// 获取Post过来的参数和数据
  67.         /// </summary>
  68.         /// <returns></returns>
  69.         public List<HttpListenerPostValue> GetHttpListenerPostValue()
  70.         {
  71.             try
  72.             {
  73.                 List<HttpListenerPostValue> HttpListenerPostValueList = new List<HttpListenerPostValue>();
  74.                 if (request.Request.ContentType.Length > 20 && string.Compare(request.Request.ContentType.Substring(0, 20), "multipart/form-data;", true) == 0)
  75.                 {
  76.                     string[] HttpListenerPostValue = request.Request.ContentType.Split(';').Skip(1).ToArray();
  77.                     string boundary = string.Join(";", HttpListenerPostValue).Replace("boundary=", "").Trim();
  78.                     byte[] ChunkBoundary = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
  79.                     byte[] EndBoundary = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
  80.                     Stream SourceStream = request.Request.InputStream;
  81.                     var resultStream = new MemoryStream();
  82.                     bool CanMoveNext = true;
  83.                     HttpListenerPostValue data = null;
  84.                     while (CanMoveNext)
  85.                     {
  86.                         byte[] currentChunk = ReadLineAsBytes(SourceStream);
  87.                         if (!Encoding.UTF8.GetString(currentChunk).Equals("\r\n"))
  88.                             resultStream.Write(currentChunk, 0, currentChunk.Length);
  89.                         if (CompareBytes(ChunkBoundary, currentChunk))
  90.                         {
  91.                             byte[] result = new byte[resultStream.Length - ChunkBoundary.Length];
  92.                             resultStream.Position = 0;
  93.                             resultStream.Read(result, 0, result.Length);
  94.                             CanMoveNext = true;
  95.                             if (result.Length > 0)
  96.                                 data.datas = result;
  97.                             data = new HttpListenerPostValue();
  98.                             HttpListenerPostValueList.Add(data);
  99.                             resultStream.Dispose();
  100.                             resultStream = new MemoryStream();

  101.                         }
  102.                         else if (Encoding.UTF8.GetString(currentChunk).Contains("Content-Disposition"))
  103.                         {
  104.                             byte[] result = new byte[resultStream.Length - 2];
  105.                             resultStream.Position = 0;
  106.                             resultStream.Read(result, 0, result.Length);
  107.                             CanMoveNext = true;
  108.                             data.name = Encoding.UTF8.GetString(result).Replace("Content-Disposition: form-data; name="", "").Replace(""", "").Split(';')[0];
  109.                             resultStream.Dispose();
  110.                             resultStream = new MemoryStream();
  111.                         }
  112.                         else if (Encoding.UTF8.GetString(currentChunk).Contains("Content-Type"))
  113.                         {
  114.                             CanMoveNext = true;
  115.                             data.type = 1;
  116.                             resultStream.Dispose();
  117.                             resultStream = new MemoryStream();
  118.                         }
  119.                         else if (CompareBytes(EndBoundary, currentChunk))
  120.                         {
  121.                             byte[] result = new byte[resultStream.Length - EndBoundary.Length - 2];
  122.                             resultStream.Position = 0;
  123.                             resultStream.Read(result, 0, result.Length);
  124.                             data.datas = result;
  125.                             resultStream.Dispose();
  126.                             CanMoveNext = false;
  127.                         }
  128.                     }
  129.                 }
  130.                 return HttpListenerPostValueList;
  131.             }
  132.             catch (Exception ex)
  133.             {
  134.                 return null;
  135.             }
  136.         }

  137.         //使用方法
  138.         //获取Post请求中的参数和值帮助类
  139.         //HttpListenerPostParaHelper httppost = new HttpListenerPostParaHelper(request);
  140.         ////获取Post过来的参数和数据
  141.         //List<HttpListenerPostValue> lst = httppost.GetHttpListenerPostValue();
  142.         //string userName = "";
  143.         //string password = "";
  144.         //string suffix = "";
  145.         //string adType = "";
  146.         //foreach (var key in lst)
  147.         //{
  148.         //    if (key.type == 0)
  149.         //    {
  150.         //        string value = Encoding.UTF8.GetString(key.datas).Replace("\r\n", "");
  151.         //        if (key.name == "username")
  152.         //        {
  153.         //            userName = value;
  154.         //            Console.WriteLine(value);
  155.         //        }
  156.         //        if (key.name == "password")
  157.         //        {
  158.         //            password = value;
  159.         //            Console.WriteLine(value);
  160.         //        }
  161.         //        if (key.name == "suffix")
  162.         //        {
  163.         //            suffix = value;
  164.         //            Console.WriteLine(value);
  165.         //        }
  166.         //        if (key.name == "adtype")
  167.         //        {
  168.         //            adType = value;
  169.         //            Console.WriteLine(value);
  170.         //        }   
  171.         //    }
  172.         //    if (key.type == 1)
  173.         //    {
  174.         //        string fileName = request.Request.QueryString["FileName"];
  175.         //        if (!string.IsNullOrEmpty(fileName))
  176.         //        {
  177.         //            string filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyMMdd_HHmmss_ffff") + Path.GetExtension(fileName).ToLower();
  178.         //            if (key.name == "File")
  179.         //            {
  180.         //                FileStream fs = new FileStream(filePath, FileMode.Create);
  181.         //                fs.Write(key.datas, 0, key.datas.Length);
  182.         //                fs.Close();
  183.         //                fs.Dispose();
  184.         //            }
  185.         //        }
  186.         //    }
  187.         //}

  188.         public byte[] ReadLineAsBytes()
  189.         {
  190.             
  191.             var resultStream = new MemoryStream();
  192.             while (true)
  193.             {
  194.                 int data = this.request.Request.InputStream.ReadByte();
  195.                 resultStream.WriteByte((byte)data);
  196.                 if (data <= 10)
  197.                     break;
  198.             }
  199.             resultStream.Position = 0;
  200.             byte[] dataBytes = new byte[resultStream.Length];
  201.             resultStream.Read(dataBytes, 0, dataBytes.Length);
  202.             return dataBytes;
  203.         }
  204.     }
  205. }
复制代码






上一篇:c# http用HttpListener监听线程
下一篇:sql如何查某个表某个字段的数据类型?
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-7-12 18:41

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表