Search code examples
c#.neturlencodegoogle-search-console

Webmaster Tools API C# - Url Encoded SiteID causing 400 response


The Webmaster Tools API requires a SiteID for most operations.
This SiteID is a Url Encoded version of the site's url, as appears in the Google Webmaster Tools dashboard.

So why does the next URL doesn't work (the dreaded "Bad Request", or "Site Not Found")?

var site = "http://example.com/";
var urlEncoded = HttpUtility.UrlEncode(site);
var url = "https://www.google.com/webmasters/tools/feeds/" + urlEncoded + "/crawlissues/";

Solution

  • Google expects upper case letters for the encoded characters, while HttpUtility.UrlEncode produces lower case characters.

    See this answer for a "selective ToUpper" method implementation.

    (Another thing, the last slash might make a difference! http://x.com/ is not http://x.com)