【发布时间】:2014-08-25 21:47:07
【问题描述】:
UriBuilder.Query 属性“包含 URI 中包含的任何查询信息。” According to the docs, "查询信息根据 RFC 2396 进行转义。"
基于此,并且由于此属性是可写的,我假设当您设置它时,System.UriBuilder 将解析您的查询字符串,并根据 RFC 2396 进行转义(url 编码)。特别是 { 和 } 是不在未保留的字符集中,所以they should be escaped according to page 9 of RFC 2396。但是,System.UriBuilder 似乎没有进行任何转义。
我需要手动 Server.URLEncode 参数,还是有办法让System.UriBuilder 处理编码?
这是我的示例代码。你可以run this on ideone.com and see that, indeed, nothing is URL encoded。
using System;
public class Test
{
public static void Main()
{
var baseUrl = new System.Uri("http://www.bing.com");
var builder = new System.UriBuilder(baseUrl);
string name = "param";
string val = "{'blah'}";
builder.Query = name + "=" + val;
// Try several different ouput methods; none will be URL encoded
Console.WriteLine(builder.ToString());
Console.WriteLine(builder.Uri.ToString());
Console.WriteLine(builder.Query);
}
}
【问题讨论】:
-
我看不到任何可以执行任何类型转换的明显代码。我想知道文档是否措辞非常糟糕,应该说该值应该根据 RFC2396 进行转义。
-
是的,好吧,当文档说查询被转义时,它们意味着 Uri 对象的 Query 属性在读取时包含转义数据。如果您自己设置此数据,则必须首先为其提供转义数据。如果它为您转义了数据,那将导致极易出错的
+=工作流。 -
@Damien_The_Unbeliever,我想知道文档中的“查询信息被转义”是否应该是“查询信息应该被转义”,或者更清楚地说,“你应该在写作之前转义查询信息它到这个属性。”