URL Encode all non alpha numeric in C#

后端 未结 4 1376
轻奢々
轻奢々 2021-01-12 11:01

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 ur

4条回答
  •  囚心锁ツ
    2021-01-12 11:35

    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

提交回复
热议问题