假设以下网址:
"http://server/application1/TestFile.aspx?Library=Testing&Filename=Documents & Functions + Properties.docx&Save=true"
我使用 HttpUtility.UrlEncode() 对 Filename 参数的值进行编码,并创建以下 Url:
"http://server/application1/TestFile.aspx?Library=Testing&Filename=Documents%20%26%20Functions%20%2B%20Properties.docx&Save=true"
我将以下请求(编码版本)从客户端发送到 C# Web 应用程序。当我处理请求时在服务器上遇到问题。 HttpRequest 变量包含部分解码的查询字符串。也就是说,当我尝试使用或快速查看 HttpRequest 的以下属性时,它们具有以下值。
Property = Value
================
HttpRequest.QueryString = "{Library=Testing&Filename=Documents+&+Functions+++Properties.docx&Save=true}"
HttpRequest.Url = "{http://server/application1/TestFile.aspx?Library=Testing&Filename=Documents & Functions + Properties.docx&Save=true}"
HttpRequest.Url.AbsoluteUri = "http://server/application1/TestFile.aspx?Library=Testing&Filename=Documents%20&%20Functions%20+%20Properties.docx&Save=true"
我还检查了以下属性,但所有属性的 & 值都已解码。然而,所有其他值都保持正确编码(例如,空格是 %20)。
HttpRequest.Url.OriginalString
HttpRequest.Url.Query
HttpRequest.Url.PathAndQuery
HttpRequest.RawUrl
我无法正确读取参数文件名的值。我错过了什么吗?
最佳答案
QueryString
属性返回一个 NameValueCollection
对象,它将查询字符串键映射到完全解码的值。
您需要编写 Request.QueryString["FileName"]
。
关于C# ASP.NET HttpWebRequest 自动解码查询字符串中的符号 (&) 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3667902/