zoukankan      html  css  js  c++  java
  • 根据指定的编码格式返回请求的参数集合

    在平时的应用中,经常需要通过post或者get方式接收来自于不同站点的url请求,而请求的编码格式又可能会有所不同,因此实现了如下的方法,用于解决用指定的编码从post或者get中提取用户请求的参数。

    有关用户编码方式,可能受到用户请求方式的不同(如纯粹的url请求,或者在文档中的form请求),会导致相应的编码方式的不同,由于没有找到对编码识别的好的办法,目前本人采用的方式基本是要求用户传入一个encode的参数用于说明请求的编码方式是什么样子的,服务方接收后就按照这个编码来提取请求数据。

     1        /// <summary>
     2        /// 根据指定的编码格式返回请求的参数集合
     3        /// </summary>
     4        /// <param name="request">请求的字符串</param>
     5        /// <param name="encode">编码模式</param>
     6        /// <returns></returns>

     7        public static NameValueCollection GetRequestParameters(HttpRequest request, Encoding encode)
     8        {
     9            NameValueCollection nv = null;
    10            if (request.HttpMethod == "POST")
    11            {
    12                if (null != encode)
    13                {
    14                    Stream resStream = request.InputStream;
    15                    byte[] filecontent = new byte[resStream.Length];
    16                    resStream.Read(filecontent, 0, filecontent.Length);
    17                    string postquery = Encoding.Default.GetString(filecontent);
    18                    nv = HttpUtility.ParseQueryString(postquery, encode);
    19                }

    20                else
    21                    nv = request.Form;
    22            }

    23            else
    24            {
    25                if (null != encode)
    26                {
    27                    nv = System.Web.HttpUtility.ParseQueryString(request.Url.Query, encode);
    28                }

    29                else
    30                {
    31                    nv = request.QueryString;
    32                }

    33            }

    34            return nv;
    35        }

    36
    37

  • 相关阅读:
    新加坡
    android alt + /
    豌豆荚开源技术
    有意思的
    android view
    localstorage性能
    Android WebView使用基础
    关于hash
    Android 近百个项目的源代码,覆盖Android开发的每个领域
    10个经典的Android开源项目(附源码包)
  • 原文地址:https://www.cnblogs.com/mincyw/p/1279293.html
Copyright © 2011-2022 走看看