Example #1
0
        private bool? CheckDomain(ICheckApi checkApi, Tuple<string, string> domain)
        {
            Debug.Assert(checkApi != null, "_checkApi != null");

            bool? result = null;
            Uri uri = new Uri(string.Format(checkApi.Url, domain.ToStringExt()));

            WebRequest request = WebRequest.Create(uri);
            request.Method = "GET";
            using (var rsp = (HttpWebResponse)request.GetResponse())
            {
                if (rsp.StatusCode == HttpStatusCode.OK)
                {
                    using (var s = rsp.GetResponseStream())
                    {
                        Debug.Assert(s != null, "s != null");
                        var sr = new StreamReader(s);
                        string tmp = sr.ReadToEnd();
                        var pattern = (domain.Item1 + domain.Item2).Replace(".", "\\.");
                        var reUnavailable = new Regex(string.Format(checkApi.PatternUnavailable, pattern));
                        if (reUnavailable.IsMatch(tmp))
                        {
                            result = false;
                        }
                        else
                        {
                            var reAvailable = new Regex(string.Format(checkApi.PatternAvailable, pattern));
                            if (reAvailable.IsMatch(tmp))
                            {
                                result = true;
                            }
                            else
                            {
                                // result can not assigned to be false here, because it may be Unknown.
                            }
                        }
                    }
                }
                else
                {
                    _errorStream.WriteLine("WebRequest Error: Domain = {0}, StatusCode = {1}, Url = {2}", domain, rsp.StatusCode, uri);
                }
            }
            return result;
        }