public async Task <bool> Check(string word) { word = word.ToLower(); //normalize if (WordCache.ContainsKey(word)) { return(WordCache.Value(word)); //if known word, skip web stuff } //https://www.dictionary.com/browse/<word> use status code to save time... maybe? var request = (HttpWebRequest)WebRequest.Create($"https://www.dictionary.com/browse/{word}"); request.Method = "HEAD"; //HEAD method doesn't return a body try { using (var response = await request.GetResponseAsync() as HttpWebResponse) { return(ProcessResponse(word, (int)response.StatusCode)); } } catch (WebException ex) //404 throws { var response = (HttpWebResponse)ex.Response; if (response == null) { throw (ex); //if something else went wrong } return(ProcessResponse(word, (int)response.StatusCode)); } catch (Exception ex) { throw (ex); // throw real exceptions } }
private bool EvalStatus(int status) => status >= 200 && status < 300; //anything in the 200 range is good private bool ProcessResponse(string word, int status) { WordCache.Add(word, EvalStatus(status)); //cache new words return(EvalStatus(status)); //anything in the 200 range is good }