//<summary> //Метод GetTextFromPage //получает URL на входе //и отдаёт весь видимый текст на странице //</summary> //<param name="uri">Аргумент метода GetTextFromPage()</param> public async Task <UriResult> GetTextFromPage(string uri) { var uriResult = new UriResult { Uri = uri }; //получение уникальных слов var text = GetUniqueWords(uriResult.Uri); uriResult.Result = text.Split(';'); //обновление данных в бд try { await UpdateData(uri, text); } catch (Exception e) { _logger.LogError(e.Message, uri); } return(uriResult); }
public bool TestResult() { bool result = true; this.Result = new UriResult(); using (WebClient client = new WebClient()) { try { if (this.WindowsAuth == true && string.IsNullOrWhiteSpace(this.User)) { client.UseDefaultCredentials = true; } if (this.WindowsAuth == true && !string.IsNullOrWhiteSpace(this.User)) { CredentialCache cc = new CredentialCache(); cc.Add( new Uri(this.Uri), "NTLM", new NetworkCredential( this.User.Contains(@"\") ? this.User.Split('\\')[1] : this.User, this.Password, this.User.Contains(@"\") ? this.User.Split('\\')[0] : System.Environment.UserDomainName)); client.Credentials = cc; } this.Result.Content = client.DownloadString(this.Uri); this.Result.StatusCode = HttpStatusCode.OK; } catch (System.Net.WebException webEx) { if (webEx.Response is System.Net.HttpWebResponse) { this.Result.StatusCode = ((System.Net.HttpWebResponse)webEx.Response).StatusCode; } this.Result.Exception = webEx; } catch (ArgumentNullException argEx) { this.Result.Content = ""; this.Result.StatusCode = HttpStatusCode.BadRequest; this.Result.Exception = argEx; result = false; } catch (NotSupportedException notEx) { this.Result.Content = ""; this.Result.StatusCode = HttpStatusCode.BadRequest; this.Result.Exception = notEx; result = false; } catch (UriFormatException uriEx) { this.Result.Content = ""; this.Result.StatusCode = HttpStatusCode.BadRequest; this.Result.Exception = uriEx; result = false; } catch (Exception ex) { this.Result.Exception = ex; result = false; } } //Validation if (!string.IsNullOrEmpty(this.ValidationRegex)) { if (!(new Regex(this.ValidationRegex)).Match(this.Result.Content).Success) { this.TestResultMessage = "HTTP Content failed to match validation Regular Expression"; result = false; } } if ((int)this.Result.StatusCode != this.StatusCode) { this.TestResultMessage = string.Format("HTTP Status Code: {0}, Expected: {1}", (int)this.Result.StatusCode, this.StatusCode); result = false; } return(result); }