bool Redirect (WebAsyncResult result, HttpStatusCode code)
{
redirects++;
Exception e = null;
string uriString = null;
switch (code) {
case HttpStatusCode.Ambiguous: // 300
e = new WebException ("Ambiguous redirect.");
break;
case HttpStatusCode.MovedPermanently: // 301
case HttpStatusCode.Redirect: // 302
case HttpStatusCode.TemporaryRedirect: // 307
/* MS follows the redirect for POST too
if (method != "GET" && method != "HEAD") // 10.3
return false;
*/
contentLength = -1;
bodyBufferLength = 0;
bodyBuffer = null;
if (code != HttpStatusCode.TemporaryRedirect)
method = "GET";
uriString = webResponse.Headers ["Location"];
break;
case HttpStatusCode.SeeOther: //303
method = "GET";
uriString = webResponse.Headers ["Location"];
break;
case HttpStatusCode.NotModified: // 304
return false;
case HttpStatusCode.UseProxy: // 305
e = new NotImplementedException ("Proxy support not available.");
break;
case HttpStatusCode.Unused: // 306
default:
e = new ProtocolViolationException ("Invalid status code: " + (int) code);
break;
}
if (e != null)
throw e;
if (uriString == null)
throw new WebException ("No Location header found for " + (int) code,
WebExceptionStatus.ProtocolError);
Uri prev = actualUri;
try {
actualUri = new Uri (actualUri, uriString);
} catch (Exception) {
throw new WebException (String.Format ("Invalid URL ({0}) for {1}",
uriString, (int) code),
WebExceptionStatus.ProtocolError);
}
hostChanged = (actualUri.Scheme != prev.Scheme || Host != prev.Authority);
return true;
}