How to judge whether a webpage existed in a fast way

---
date: Mar 09, 2016
tags:
- C#
language: English
---

Recently I am coding a app for viewing newspaper. Because there are no API, I have to manually find whether the page ever existed. So I try to look on the Internet, and find a try catch way to judge it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
HttpWebResponse response = null;
var request = (HttpWebRequest)WebRequest.Create(/* url */);
request.Method = “HEAD”;

try{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex){
/* A WebException will be thrown if the status of the response is not `200 OK` */
}
finally{
// Don’t forget to close your response.
if (response != null)
{
response.Close()
}
}

However, I find that GetResponse method now no longer existed in .NET framework 4.5.2. So I find a another one that worked. But it is very slow. So I come up with an idea-using the HTTP head to find out whether it exists, and it is a lot faster:

1
2
3
4
5
6
7
8
9
HttpClient httpClient = new HttpClient();

//Getting the Web Response.
HttpResponseMessage httpResponse = new HttpResponseMessage();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(website));
httpResponse = await httpClient.SendRequestAsync(request);
if(httpResponse.ToString().Contains(“StatusCode: 404”)){
// do something when the page doesn’t exist
}

real working code

Navigation


Creative Commons License

Unless specified, all the articles are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


It uses a self-hosted Open Web Analytics to get insights. Click here to optout.

The site is powered by Hexo. The source code of this website is here, so that you can see the spaghetti code behind my blog.


Main | Blog | Comic | Podcasts (Preparing) | RSS | Sitemap
2008 - 2022 | Patrick WU Jinming