0

I have to make a call to an API and the URL hasd this format: http://apiurl.com/method/parameter1/parameter2

Now, parameter2 may contain characters that could mess with the URL such as forward slashes, so I URL encode parameter2:

string parameter2 = HttpUtility.UrlEncode("string with a / in it");
System.Net.WebRequest request = System.Net.WebRequest.Create(String.Format("http://apiurl.com/method/{0}/{1}", parameter1, parameter2));

The problem is that if I inspect the URL of the request, parameter2 is NOT URL encoded and the call fails (unsurprisingly). However, when I inspect the value of parameter2 it is correctly URL encoded with the forward slash replaced with %2f

I am using .NET 3.5 so many of the usual workarounds don't apply (genericUriParserOptions for example is .NET 4 and above only)

Thanks

tony.wiredin
  • 617
  • 8
  • 21
  • Nothing. I tried to reproduce it, but I can't. The RequestUri-Property is correct and the outgoing request (captured with fiddler) is correct, too. Can you give us more information? Is your pasted minimal example identical with your application code (maybe the simplification stripped the wrong behavior). – Sven-Michael Stübe Dec 18 '13 at 07:50
  • This code is identical to actual code except for the URLs and the parameters. This is a known problem so it's not just me - see http://stackoverflow.com/questions/10413795/c-sharp-webrequest-getresponse-400-bad-request/10415482#10415482 the workaround mentioned here doesn't work in .NET 3.5. And the actual URL used is the unescaped version, BTW – tony.wiredin Dec 18 '13 at 07:53
  • did you try to encode it twice? HttpUtility.UrlEncode(HttpUtility.UrlEncode("with a / in it")); – Sven-Michael Stübe Dec 18 '13 at 08:43

0 Answers0