Search code examples
c#-4.0url-encoding

WebRequest.Create unencoding backslashes in path


I have a rest service that requires passing an encrypted key as part of the path. I urlencode the key and it works great when just placed in the browser. However, in my code I user WebRequest.Create and that appears to replace any backslashes that are generated by the encryption key. This results in the service thinking that it part of the route and fails with a 404. IS this a known defect in the .net framework or am I missing something? Seems like a pretty big deal.

Edit: (Simplified sample code)

string key = System.Web.HttpUtility.UrlEncode(TripleDESEncode("sharedkey")); string uri = string.Format("http://mydomail.com/deposit/{0}.{1}", key, "json");

//uri looks like this here http://mydomail.com/deposit/FHnapfF5yBCEKt3%2f3YOQ5g%3d%3d.json

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);

//Now the address in the HttpWebRequest is this... //http://mydomail.com/deposit/FHnapfF5yBCEKt3/3YOQ5g%3d%3d.json

Hopefully this helps.


Solution

  • Ok, I ended-up making a compromise with my client and skipped the encryption for straight serializing to base64. This was only acceptable due to the nature of what I am passing. encrytion will be required in the future and I see this as a major problem that needs to be fixed. At least a workaround proposed. If I come across one I will post it.

    Thanks everyone!

    Final code:

    HttpUtil.UrlEncode(Convert.ToBase64String(Encoding.UTF8.GetBytes("sharedKey")));