URL Encode all non alpha numeric in C#

て烟熏妆下的殇ゞ 提交于 2019-12-01 01:43:18

问题


I need to fully URL Encode an email address.

HttpUtility.UrlEncode seems to ignore certain characters such as ! and .

I need to pass an email address in a url formated like this:

/Users/me@example.com/Comments

Because my WebMethod uri template looks like this:

[WebGet(UriTemplate = "Users/{emailAddress}/Comments")]

The period breaks WCF and will not pass the email address to my REST webservice method. Removing the period passes the value just fine. I'm hoping there is a method which will encode all non alpha numeric characters since everything consuming this service will need to do this.

EDIT

I had considered using:

Convert.ToBase64String(Encoding.ASCII.GetBytes("something+me@example.com"))

Do most other languages have easy ways to convert a string to base64? My main concern is that our customers who consume this service will need to encode the email address using Java, PHP, Ruby, etc.


回答1:


Here's a potential Regex you could use to accomplish the encoding.

Regex.Replace(s, @"[^\w]", m => "%" + ((int)m.Value[0]).ToString("X2"));

I'm not sure that there is an existing framework method defined to strictly encode all non-alphanumeric characters that you could point your clients to.




回答2:


Use hex. There is a ConvertTo and a From and a sample test...

You can also just scape the characters that don't match A to Z by using a regex so your URL still looks pretty.

It will return a big list of numbers so you should be good

        public static string ConvertToHex(string asciiString)
    {
        var hex = "";
        foreach (var c in asciiString)
        {
            int tmp = c;
            hex += String.Format("{0:x2}", Convert.ToUInt32(tmp.ToString()));
        }
        return hex;
    }

    public static string ConvertToString(string hex)
    {
        var stringValue = "";
        // While there's still something to convert in the hex string
        while (hex.Length > 0)
        {
            stringValue += Convert.ToChar(Convert.ToUInt32(hex.Substring(0, 2), 16)).ToString();
            // Remove from the hex object the converted value
            hex = hex.Substring(2, hex.Length - 2);
        }

        return stringValue;
    }

    static void Main(string[] args)
    {
        string hex = ConvertToHex("don.joe@gmail.com");
        Console.WriteLine(hex);
        Console.ReadLine();
        string stringValue =
        ConvertToString(hex);
        Console.WriteLine(stringValue);
        Console.ReadLine();

    }



回答3:


Unless I am mistaken, URL Encoding is simply the percent sign followed by the ASCII number (in hex), so this should work...

Dim Encoded as New StringBuilder()

For Each Ch as Char In "something+me@example.com"
    If Char.IsLetterOrDigit(Ch)
         Encoded.Append(Ch)
    Else
         Encoded.Append("%")
         Dim Byt as Byte = Encoding.ASCII.GetBytes(Ch)(0)
         Encoded.AppendFormat("{0:x2}", Byt)
    End If
Next

The above code results in something%2Bme%40example.com




回答4:


I've discovered a solution to this issue.

.Net 4.0 has actually fixed the problem with special characters in the URI Template.

This thread pointed me in the right direction. http://social.msdn.microsoft.com/Forums/en/dataservices/thread/b5a14fc9-3975-4a7f-bdaa-b97b8f26212b

I added all the config settings and it worked. But note, it ONLY works with a REAL IIS setup with .Net 4.0. I can't seem to get it to work with the Visual Studio Dev IIS.

Update - Actually, I tried removing those config settings and it still works. It maybe be that .Net 4.0 has fixed this problem by default.



来源:https://stackoverflow.com/questions/3072409/url-encode-all-non-alpha-numeric-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!