url参数转成NameValueCollection

public static NameValueCollection GetRequestParameters(HttpRequest request,string encode)
        {
            NameValueCollection nv = null;
            Encoding destEncode = null;
            if (!String.IsNullOrEmpty(encode))
            {
                try
                {
                    destEncode = Encoding.GetEncoding(encode);
                }
                catch { }
            }

            if (request.HttpMethod == "POST")
            {
                if (null != destEncode)
                {
                    Stream resStream = request.InputStream;
                    byte[] filecontent = new byte[resStream.Length];
                    resStream.Read(filecontent, 0, filecontent.Length);
                    string postquery = Encoding.Default.GetString(filecontent);
                    nv = HttpUtility.ParseQueryString(postquery, destEncode);
                }
                else
                    nv = request.Form;
            }
            else
            {
                if (null != destEncode)
                {
                    nv = System.Web.HttpUtility.ParseQueryString(request.Url.Query, destEncode);
                }
                else
                {
                    nv = request.QueryString;
                }
            }
            return nv;
        }

posted @ 2008-12-22 18:15  roboth  阅读(603)  评论(0编辑  收藏  举报