如何替换URL中的特殊字符?

时间:2021-11-29 08:37:00

This is probably very simple, but I simply cannot find the answer myself :(

这可能很简单,但我自己找不到答案:(

Basicaly, what I want is, given this string:

基本上,我想要的是,给定这个字符串:

"http://www.google.com/search?hl=en&q=c# objects"

I want this output:

我想要这个输出:

http://www.google.com/search?hl=en&q=c%23+objects

I'm sure there's some helper class somewhere buried in the Framework that takes care of that for me, but I'm having trouble finding it.

我确信在框架中埋藏了一些辅助类,它可以解决这个问题,但是我找不到它。

EDIT: I should add, that this is for a Winforms App.

编辑:我应该补充一点,这是一个Winforms应用程序。

4 个解决方案

#1


HttpServerUtility.UrlEncode(string)

Should sort out any troublesome characters

应该解决任何麻烦的人物

To use it you'll need to add a reference to System.Web (Project Explorer > References > Add reference > System.Web)

要使用它,您需要添加对System.Web的引用(Project Explorer>引用>添加引用> System.Web)

Once you've done that you can use it to encode any items you wish to add to the querystring:

完成后,您可以使用它来编码要添加到查询字符串的任何项目:

System.Web.HttpUtility.UrlEncode("c# objects");

#2


If you don't want a dependency on System.Web here is an implementation of "UrlEncode" I have in my C# OAuth Library (which requires a correct implementation - namely spaces should be encoded using percent encoding rather the "+" for spaces etc.)

如果你不想依赖System.Web这里是我在C#OAuth库中的“UrlEncode”的实现(需要正确的实现 - 即空格应使用百分比编码而不是“+”用于空格等。)

private readonly static string reservedCharacters = "!*'();:@&=+$,/?%#[]";

public static string UrlEncode(string value)
{
    if (String.IsNullOrEmpty(value))
        return String.Empty;

    var sb = new StringBuilder();

    foreach (char @char in value)
    {
        if (reservedCharacters.IndexOf(@char) == -1)
            sb.Append(@char);
        else
            sb.AppendFormat("%{0:X2}", (int)@char);
    }
    return sb.ToString();
}

For reference http://en.wikipedia.org/wiki/Percent-encoding

供参考http://en.wikipedia.org/wiki/Percent-encoding

#3


@Wilfred Knievel has the accepted answer, but you could also use Uri.EscapeUriString() if you wanted to avoid the dependency on the System.Web namespace.

@Wilfred Knievel有接受的答案,但如果你想避免依赖System.Web命名空间,你也可以使用Uri.EscapeUriString()。

#4


Look at Server.UrlEncode

查看Server.UrlEncode

http://msdn.microsoft.com/en-us/library/ms525738.aspx

#1


HttpServerUtility.UrlEncode(string)

Should sort out any troublesome characters

应该解决任何麻烦的人物

To use it you'll need to add a reference to System.Web (Project Explorer > References > Add reference > System.Web)

要使用它,您需要添加对System.Web的引用(Project Explorer>引用>添加引用> System.Web)

Once you've done that you can use it to encode any items you wish to add to the querystring:

完成后,您可以使用它来编码要添加到查询字符串的任何项目:

System.Web.HttpUtility.UrlEncode("c# objects");

#2


If you don't want a dependency on System.Web here is an implementation of "UrlEncode" I have in my C# OAuth Library (which requires a correct implementation - namely spaces should be encoded using percent encoding rather the "+" for spaces etc.)

如果你不想依赖System.Web这里是我在C#OAuth库中的“UrlEncode”的实现(需要正确的实现 - 即空格应使用百分比编码而不是“+”用于空格等。)

private readonly static string reservedCharacters = "!*'();:@&=+$,/?%#[]";

public static string UrlEncode(string value)
{
    if (String.IsNullOrEmpty(value))
        return String.Empty;

    var sb = new StringBuilder();

    foreach (char @char in value)
    {
        if (reservedCharacters.IndexOf(@char) == -1)
            sb.Append(@char);
        else
            sb.AppendFormat("%{0:X2}", (int)@char);
    }
    return sb.ToString();
}

For reference http://en.wikipedia.org/wiki/Percent-encoding

供参考http://en.wikipedia.org/wiki/Percent-encoding

#3


@Wilfred Knievel has the accepted answer, but you could also use Uri.EscapeUriString() if you wanted to avoid the dependency on the System.Web namespace.

@Wilfred Knievel有接受的答案,但如果你想避免依赖System.Web命名空间,你也可以使用Uri.EscapeUriString()。

#4


Look at Server.UrlEncode

查看Server.UrlEncode

http://msdn.microsoft.com/en-us/library/ms525738.aspx