Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# detect page redirect

Tags:

c#

.net

webclient

I am trying to determine if a qualification exists on http://www.accreditedqualifications.org.uk in the form:

http://www.accreditedqualifications.org.uk/qualification/50084811.seo.aspx

50084811 being a qualification aim entered by the end user.

If they enter an invalid one e.g.

http://www.accreditedqualifications.org.uk/qualification/50084911.seo.aspx

They are redirected to an error page (with incorrect http headers as far as I can see). Is there a way to detect the redirect in C#. I would hope to be able to detect the redirect in http headers (thinking it will issue 2) or similar as oppose to having to download the whole page. This could be happening a lot so I would like to minimize traffic.

Edit

Used this to have a look at the headers looks like two are issued for an invalid page:

http://pageheaders.com/display-http-headers.php?url=http%3A%2F%2Fwww.accreditedqualifications.org.uk%2Fqualification%2F50084911.seo.aspx&agent=ie6

like image 513
PeteT Avatar asked Mar 29 '10 14:03

PeteT


3 Answers

There are two ways to detect a page redirect:

  1. check if response.StatusCode == HttpStatusCode.Redirect is set in your HttpWebResponse
  2. compare request.RequestUri and response.ResponseUri

Please note that 1) depends on the implementation the server, not all servers set this status code, so option 2) might be more reliable:

HttpWebRequest request = CreateWebRequest(requestString);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
bool redirected = request.RequestUri.ToString() != response.ResponseUri.ToString();
like image 199
uceumern Avatar answered Sep 30 '22 14:09

uceumern


There are a number of different codes that could be returned. You could check the various codes a la:

response.StatusCode == HttpStatusCode.Redirect

You can view all the possibilities at http://msdn.microsoft.com/en-us/library/system.net.httpstatuscode.aspx

Alternatively, you might find it sufficient to check whether the Location in the response is different.

var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "HEAD";
request.AllowAutoRedirect = false;

string location;
using (var response = request.GetResponse() as HttpWebResponse)
{
  location = response.GetResponseHeader("Location");
}
return (location != uri.OriginalString);
like image 24
Handcraftsman Avatar answered Sep 30 '22 12:09

Handcraftsman


The simplest way is probably to fetch the content using a HEAD request (set Method to "HEAD") in an HttpWebRequest having set AllowAutoRedirect to false. I can't remember offhand whether that will cause an exception or not, but either way it should be easy to handle.

like image 36
Jon Skeet Avatar answered Sep 30 '22 14:09

Jon Skeet