11

Having a problem with HttpWebRequest decoding my encoded URL.

var requestUrl = "https://www.google.com/webmasters/tools/feeds/http%3A%2F%2Fwww%2example%2Ecom%2F/crawlissues/";   
var request = (HttpWebRequest)WebRequest.Create(requestUrl);

When looking at end request URL is becomes:

https://www.google.com/webmasters/tools/feeds/http://www.example.com//crawlissues/

Which of course returns a 400 Bad request. I am guessing it is something todo with the URI class rather than HttpWebRequest. How do I stop this from happening?

  • What is the output URL that you are expecting ? Thanks – Mahesh Velaga Feb 18 '10 at 10:00
  • The URL is meant to be https://www.google.com/webmasters/tools/feeds/http%3A%2F%2Fwww%2example%2Ecom%2F/crawlissues/ as you can see the domain is encoded and needs to stay encoded. –  Feb 18 '10 at 10:09

5 Answers5

5

This is an annoying "security feature" of the Uri class. If you're using 4.0 or later, you can turn it off in your configuration file; otherwise, you'll have to resort to reflection.

Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
1

I don't think you can request that url.

It won't decode %2F in a query parameter. So, it would work if the encoded data was in a query parameter:

requestUrl = "https://google.com/tools?feeds=http%3A%2F%2Fwww%2example%2Ecom%2F/crawlissues/";   
var request = (HttpWebRequest)WebRequest.Create(requestUrl);
Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
liammclennan
  • 5,295
  • 3
  • 34
  • 30
1

There is a much simpler way to this

var request=(HttpWebRequest)WebRequest.Create(Uri.EscapeUriString(requestUrl));
request.Headers.Add("Content-Transfer-Encoding","binary");

worked like a charm for me

0

Not sure but may be HttpServerUtility.UrlEncode method will help.

Upd. Alternatively you may use WebClient class.

sashaeve
  • 9,387
  • 10
  • 48
  • 61
  • 2
    Thanks but the url is already encoded, the problem is that something is decoding it. –  Feb 18 '10 at 10:01
  • I have exactly the same problem. Oddly a URL that is not decoded when I run under IISExpress in Visual Studios debugging the site, IS decoded when running under IIS proper. The decoding breaks the URL! – Bernd Wechner Jan 22 '23 at 23:32
0

Try to change the Request method from POST to GET