9

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

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
PeteT
  • 18,754
  • 26
  • 95
  • 132

5 Answers5

15

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);
Handcraftsman
  • 6,863
  • 2
  • 40
  • 33
14

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Found this http://stackoverflow.com/questions/924679/c-how-can-i-check-if-a-url-exists-is-valid I think it's along the correct lines but since the site doesn't issue a 404 or similar it's not working. I will keep experimenting. – PeteT Mar 29 '10 at 15:07
  • @petebob796: Using `WebClient` may not give you enough control. In particular, it doesn't allow you to turn off auto-redirect (as far as I can see). Use HttpWebRequest and look at the response code. 301 and 302 are the normal redirection response codes. – Jon Skeet Mar 29 '10 at 15:25
  • Thanks sorted now using HttpWebRequest and HttpWebResponse along with request.AllowAutoRedirect = false; and request.Method = "HEAD";. For anyone else reading I got caught out by the StatusCode for a second it returns "OK", "NOT FOUND"... but its an enum and can be cast to the actual error 301, 302... – PeteT Mar 29 '10 at 15:57
1

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();
uceumern
  • 877
  • 10
  • 14
0

You can only do that using a webclient from your server. If you give the link to your client, you will not be in part of the communication between him and the accredited qualificationsweb server afterwards, and tehrefore you will not be able to get the information that the link has been redirected to an error.

As far as I can understand your project, I would make some call using Webclient ( or what ever) from my server to be sure the qualification exists and store the result in the database with a buffering time. It would allow not to make too many calls and to get reliable information nonetheless.

Arthis
  • 2,283
  • 21
  • 32
  • This is actually a winforms project not asp.net I just need to validate the aim they enter is valid based on it being on the site. – PeteT Mar 29 '10 at 15:15
0

Try this:

try
{
     HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(line);
     myHttpWebRequest.Timeout = 20000;
     myHttpWebRequest.MaximumAutomaticRedirections = 1;
     myHttpWebRequest.AllowAutoRedirect = true;
     HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
     if (myHttpWebResponse.ResponseUri.ToString() == "Some website")
         {
              //your logic
         }

     myHttpWebResponse.Close();
}
catch (WebException)
{
     // record exception
}

It is based on the HEAD request of httpwebrequest having set AllowAutoRedirect to false.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77