URL Encoding all non-literal numbers in C #

I need a fully url encode email address.

HttpUtility.UrlEncode seems to ignore some characters, such as! and.

I need to pass the email address in the url created as follows:

/Users/me@example.com/Comments 

Since my WebMethod uri pattern looks like this:

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

The period breaks the WCF and will not pass the email address to my REST web service method. Removing a period conveys the value just fine. I hope there is a method that will encode all non-alphanumeric numeric characters, since everyone consuming this service will need to do this.

EDIT

I reviewed the use of:

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

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

+7
c # urlencode wcf
source share
4 answers

I found a solution to this problem.

.Net 4.0 really fixed the problem with special characters in the URI pattern.

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

I added all the configuration settings and it worked. But keep in mind that it ONLY works with setting up REAL IIS with .Net 4.0. I cannot get it to work with Visual Studio Dev IIS.

Update. In fact, I tried to remove these configuration settings, and it still works. It is possible that .Net 4.0 fixed this problem by default.

0
source share

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

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

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

+2
source share

Use hex. There is ConvertTo and From and Sample Test ...

You can also just copy characters that don't match AZ with a regex to make your URL look pretty pretty.

It will return a large list of numbers so you are 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 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(); } 
0
source share

If I'm wrong, URL encoding is just a percent sign followed by an ASCII number (in hexadecimal format), 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 leads to something%2Bme%40example.com

0
source share

All Articles