4

The code itself isn't complex, it's just not working properly:

Uri uri = new Uri("https://www.google.com/webmasters/tools/feeds/sites/http%3A%2F%2Fwww.mydomain.co.uk%2F");
WebRequest.Create(uri);

I get a "Bad Request" back from the server, and after much MUCH digging, discovered that the Uri is being turned into

https://www.google.com/webmasters/tools/feeds/sites/http%3A//www.mydomain.co.uk/

which is not what I asked for, and so it's having a whinge

Is there a way to stop this?

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
Paul
  • 9,409
  • 13
  • 64
  • 113
  • it shouldn't be unencoding the url, it should leave it as it is (https://developers.google.com/webmaster-tools/docs/2.0/developers_guide_protocol) – Paul Sep 17 '12 at 15:42
  • What happends if you just pass the string value into the `Create(` method? `WebRequest.Create("https://www.google.com/webmasters/tools/feeds/sites/http%3A%2F%2Fwww.mydomain.co.uk%2F");` – Tr1stan Sep 17 '12 at 15:47
  • It does the same - under the bonnet it just turns it straight in to a Uri – Paul Sep 17 '12 at 15:48
  • yeah, it's definitely the Uri that's doing. Alas adding true doesn't make any difference (it's also deprecated I think) – Paul Sep 17 '12 at 15:56
  • possible duplicate of [C# WebRequest.getResponse(): 400 Bad Request](http://stackoverflow.com/questions/10413795/c-sharp-webrequest-getresponse-400-bad-request) – Jeff Mercado Sep 17 '12 at 16:19

1 Answers1

5

Answer found here: https://stackoverflow.com/a/10415482/159341

According to the bug report for this issue on Microsoft Connect, this behaviour is by design, but you can work around it by adding the following to your app.config or web.config file:

<uri>
  <schemeSettings>
    <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes" />
  </schemeSettings>
</uri>

(Since WebRequest.Create(string) just delegates to WebRequest.Create(Uri), you would need to use this workaround no matter which method you call.)

Community
  • 1
  • 1
Tr1stan
  • 2,755
  • 1
  • 26
  • 45
  • Thank you! And good find. Alas this didn't fix my webmaster tools problem, although I thought it would (for anyone else looking for the same solution as me!). I shall soldier on though! – Paul Sep 17 '12 at 16:01