When I make a HttpWebRequest to a URL with AllowAutoRedirect set to false, I expect a response with StatusCode "Moved" or "Moved Permanently". I am using .NetFramework , and if I use .NetCore, it would throw a web exception with the "Moved Permenently" status code.
But for some reason, goo.gl short URLs which re-direct to a full URL, just return a "Found" status code. Why does this happen? I am building a short URL resolver but goo.gl short URLs aren't behaving as expected.
I am using the code below and it works as expected for other short URLs.
public static string GetFullUrl(string url)
{
if (string.IsNullOrWhiteSpace(url))
return "";
var req = (HttpWebRequest)WebRequest.Create(url);
req.AllowAutoRedirect = false;
try
{
var resp = req.GetResponse();
return resp.ResponseUri.AbsoluteUri;
}
catch (WebException ex)
{
if (ex.Response != null)
{
var longUrl = ex.Response.Headers["Location"];
if (longUrl.Contains('?') || longUrl.Length <= 35 || longUrl.StartsWith("https://news.google.com/"))
{
return GetFullUrl(longUrl);
}
return longUrl;
}
return url;
}
catch (Exception)
{
return url;
}
}
What makes you think it is unexpected? This is essentially a temporary redirect response; the intent here may be twofold:
Note that 307 ("Temporary Redirect") and 302 ("Found") have the same semantics, other than what to do about the HTTP method; 307 explicitly retains the original HTTP method ("POST", "GET", etc); 302 does not. In effect, this means that goo.gl is saying "temporary redirect, and you should feel free to do a "GET", regardless of what you did originally". Note that 301 ("Moved Permanently") and 308 ("Permanent Redirect") have the same duality, but for non-temporary redirects.