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); }
private void Checking(ICheckApi checkApi, IEnumerable<Tuple<string, string>> domains) { if (_cancel.IsCancellationRequested) { return; } if (domains != null) { var arr = domains.ToList(); int n = 0; if (File.Exists(_filePathRegisted)) File.Delete(_filePathRegisted); using (StreamWriter fileStreamRegisted = new StreamWriter(_filePathRegisted) , fileStreamUnRegisted = new StreamWriter(_filePathUnregisted) , fileStreamUnKnown = new StreamWriter(_filePathUnknown)) { for (int i = 0;i < arr.Count;i++) { if (_cancel.IsCancellationRequested) { break; } ShowProgress(i, arr.Count, string.Format("Checking...{0}, {{0}}/{{1}}", arr[i].ToStringExt())); bool? isOk = CheckDomain(checkApi, arr[i]); var msg = arr[i].ToStringExt(); switch (isOk) { case false: { fileStreamRegisted.WriteLine(msg); AddItem(lbResultRegisted, msg); } break; case true: { n++; fileStreamUnRegisted.WriteLine(msg); AddItem(lbResultUnRegisted, msg); } break; default: { fileStreamUnKnown.WriteLine(msg); AddItem(lbResultUnknown, msg); } break; } Thread.Sleep(100); } } var state = string.Format("Done. Got {0} domains unregisted.", n); _sc.Post(delegate { pb.ContentStringFormat = state; Title = string.Format("{0}, {1}", STR_TITLE, state); }, null); } else { throw new BzException(STR_ERR_GENERATE_DOMAIN_FIRST); } }
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; }
private void Checking(ICheckApi checkApi, IEnumerable <Tuple <string, string> > domains) { if (_cancel.IsCancellationRequested) { return; } if (domains != null) { var arr = domains.ToList(); int n = 0; if (File.Exists(_filePathRegisted)) { File.Delete(_filePathRegisted); } using (StreamWriter fileStreamRegisted = new StreamWriter(_filePathRegisted) , fileStreamUnRegisted = new StreamWriter(_filePathUnregisted) , fileStreamUnKnown = new StreamWriter(_filePathUnknown)) { for (int i = 0; i < arr.Count; i++) { if (_cancel.IsCancellationRequested) { break; } ShowProgress(i, arr.Count, string.Format("Checking...{0}, {{0}}/{{1}}", arr[i].ToStringExt())); bool?isOk = CheckDomain(checkApi, arr[i]); var msg = arr[i].ToStringExt(); switch (isOk) { case false: { fileStreamRegisted.WriteLine(msg); AddItem(lbResultRegisted, msg); } break; case true: { n++; fileStreamUnRegisted.WriteLine(msg); AddItem(lbResultUnRegisted, msg); } break; default: { fileStreamUnKnown.WriteLine(msg); AddItem(lbResultUnknown, msg); } break; } Thread.Sleep(100); } } var state = string.Format("Done. Got {0} domains unregisted.", n); _sc.Post(delegate { pb.ContentStringFormat = state; Title = string.Format("{0}, {1}", STR_TITLE, state); }, null); } else { throw new BzException(STR_ERR_GENERATE_DOMAIN_FIRST); } }