本文介绍了为什么不UriBuilder.query逃脱(URL编码)的查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UriBuilder.Query 属性包含包含在URI中的任何查询信息。 根据文档,查询信息根据RFC 2396逃走了。

在此基础上,并且因为这个属性是可写的,我认为,当你设置, System.UriBuilder 将解析您的查询字符串,和逃生(URL连接$ C $三)根据RFC 2396特别是,{和}不在未保留的字符集,并因此他们应该根据RFC 2396 9页进行转义。但是,似乎 System.UriBuilder 是没有做任何转义。

我是否需要手动Server.URLEn $ c中的PARAMS C $,还是有办法让 System.UriBuilder 处理编码?

下面是我的示例code。您可以上ideone.com运行这个,看看那个,真的,没有什么是URL连接codeD

 使用系统;

公共类测试
{
    公共静态无效的主要()
    {
        VAR的baseUrl =新的System.Uri(http://www.bing.com);
        VAR建设者=新System.UriBuilder(的baseUrl);
        字符串名称=参数;
        字符串VAL ={'嗒嗒'};
        builder.Query =名称+=+ VAL;

        //尝试几种不同的输出中的方法;没有人会进行URL连接codeD
        Console.WriteLine(builder.ToString());
        Console.WriteLine(builder.Uri.ToString());
        Console.WriteLine(builder.Query);
    }
}
 

解决方案

  builder.Uri.AbsoluteUri
 

就是你要寻找的,而你的情况,返回

的机器人

http://www.bing.com/?param=%7B'blah'%7D

鉴于困难,了解是否&安培; + = 符号应该是连接codeD或不是,它可能会更好做自己的逃逸,当你分配给 .Query 属性。

The UriBuilder.Query property "contains any query information included in the URI." According to the docs, "the query information is escaped according to RFC 2396."

Based on this, and since this property is writable, I assumed that when you set it, System.UriBuilder would parse your query string, and escape (url encode) according to RFC 2396. In particular, the { and } are not in the unreserved character set, and so they should be escaped according to page 9 of RFC 2396. But, it appears that System.UriBuilder is not doing any escaping.

Do I need to manually Server.URLEncode the params, or is there a way to get System.UriBuilder to handle the encoding?

Here's my sample code. You can 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);
    }
}
解决方案
builder.Uri.AbsoluteUri

is the droid you're looking for, which in your case, returns

http://www.bing.com/?param=%7B'blah'%7D

Given the difficulties with knowing whether the &, + or = symbol should be encoded or not, it's probably better to do your own escaping when you assign to the .Query property.

这篇关于为什么不UriBuilder.query逃脱(URL编码)的查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 18:19