title: C# WebRequest.getResponse(): 400 Bad Request
body: <p>I am trying to download a file from a server using System.Web.
  It actually works, but some links give me trouble. The links look like this:</p>
  
  <pre><code>http://cdn.somesite.com/r1KH3Z%2FaMY6kLQ9Y4nVxYtlfrcewvKO9HLTCUBjU8IBAYnA3vzE1LGrkqMrR9Nh3jTMVFZzC7mxMBeNK5uY3nx5K0MjUaegM3crVpFNGk6a6TW6NJ3hnlvFuaugE65SQ4yM5754BM%2BLagqYvwvLAhG3DKU9SGUI54UAq3dwMDU%2BMl9lUO18hJF3OtzKiQfrC/the_file.ext
  </code></pre>
  
  <p>The code looks basically like this:</p>
  
  <pre><code>HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
  WebResponse response = request.getResponse();
  </code></pre>
  
  <p>getResponse() always throws an exception (Error 400 Bad Request).
  However, I know the link works because I can download the file with Firefox without problems.</p>
  
  <p>I also tried to decode the link with Uri.UnescapeDataString(link), but that link wont even work in Firefox. </p>
  
  <p>Other links work perfectly fine this way.. just these won't work.</p>
  
  <h2>Edit:</h2>
  
  <p>Okay, i found something out using wireshark:</p>
  
  <p>If i open the link using Firefox, this is sent:</p>
  
  <pre><code>&amp;ME3@"dM*PNyAo PA:]GET /r1KH3Z%2FaMY6kLQ9Y4nVxYp5DyNc49t5kJBybvjbcsJJZ0IUJBtBWCgri3zfTERQught6S8ws1a%2BCo0RS5w3KTmbL7i5yytRpn2QELEPUXZTGYWbAg5eyGO2yIIbmGOcFP41WdrFRFcfk4hAIyZ7rs4QgbudzcrJivrAaOTYkEnozqmdoSCCY8yb1i22YtEAV/epd_outpost_12adb.flv HTTP/1.1
  Host: cdn.somesite.com
  User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
  Accept-Encoding: gzip, deflate
  Connection: keep-alive
  </code></pre>
  
  <p>I think only the first line is the problem, because WebRequest.Create(link) decodes the url:</p>
  
  <pre><code>&amp;MEz.@!dM/nP9@~P&gt;.GET /r1KH3Z/aMY6kLQ9Y4nVxYp5DyNc49t5kJBybvjbcsJJZ0IUJBtBWCgri3zfTERQught6S8ws1a%2BCo0RS5w3KTmbL7i5yytRpn2QELEPUXZTGYWbAg5eyGO2yIIbmGOcFP41WdrFRFcfk4hAIyZ7rs6Mmh1EsQQ4vJVYUwtbLBDNx9AwCHlWDfzfSWIHzaaIo/epd_outpost_12adb.flv HTTP/1.1
  User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0
  Host: cdn.somesite.com
  </code></pre>
  
  <p>( %2F is replaced with / )</p>
  
  <h2>Another edit:</h2>
  
  <p>I found out that the Uri class decodes the url automatically:
      Uri uri = new Uri(link); //link is not decoded
      Debug.WriteLine(uri.ToString()); //link is decoded here.</p>
  
  <p>How can I prevent this?</p>
  
  <p>Thanks in advance for your help.</p>