根据指定的编码格式返回请求的参数集合

在平时的应用中,经常需要通过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

posted @ 2008-08-29 11:15  远去的河流  阅读(379)  评论(0编辑  收藏  举报