I'm using http://dictionary.reference.com/browse/:word and check the status of returned response to evaluate whether the 'word' definition exists or not. It works fine for correct words (200 status code). But for those spelled incorrectly (404 status code), I'm unable to catch the status code from HttpWebRequest.GetResponse(), as it returns null?
The thing is I'm using PostMan to check and I did receive status code 404 there for the following url:
http://dictionary.reference.com/browse/sdfsdsdc
Here's my implementation:
string baseURL = "http://dictionary.reference.com/browse";
bool result = false;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseURL + "/" + word);
request.Method = "GET";
//request.Timeout = 2000;
request.KeepAlive = false;
request.ContentType = "text/html; charset=UTF-8";
Console.WriteLine(request.RequestUri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpStatusCode statusCode = response.StatusCode;
int statusCodeNumber = (int)statusCode;
Console.WriteLine("Status Code: " + statusCode + " - status number: " + statusCodeNumber);
try
{
if (statusCodeNumber == 200) result = true;
else if (statusCodeNumber == 404) { result = false; }
else { throw new UnknownStatusCodeException(); }
}
catch (UnknownStatusCodeException e) {}
response.Close();
}
catch (WebException e) {
}
}
Here's the WebException I caught
Could someone give me some pointers? Thanks