public async Task <SearchResult> SearchUrl(string url, CancellationToken cancellationToken = default(CancellationToken)) { if (url == null) { throw new ArgumentNullException(nameof(url)); } if (string.IsNullOrWhiteSpace(url)) { throw new ArgumentException(nameof(url)); } var httpResponse = await UseClient(async (httpClient, cT) => await httpClient.GetAsync($"?url={url}", cT), cancellationToken); httpResponse.EnsureSuccessStatusCode(); var html = await httpResponse.Content.ReadAsStringAsync(); var parser = new SearchResultParser(); return(parser.ParseResult(html)); }
public async Task <SearchResult> SearchFile(Stream fileStream, CancellationToken cancellationToken = default(CancellationToken)) { if (fileStream == null) { throw new ArgumentNullException(nameof(fileStream)); } if (fileStream.Length > 8388608) { throw new ImageTooLargeException(); } var form = GetFromDataContent(fileStream); var response = await UseClient(async (httpClient, cT) => await httpClient.PostAsync("/", form, cT), cancellationToken); try { response.EnsureSuccessStatusCode(); } catch (HttpRequestException e) { if (response.StatusCode == HttpStatusCode.RequestEntityTooLarge) { throw new ImageTooLargeException(innerException: e); } throw; } var html = await response.Content.ReadAsStringAsync(); var parser = new SearchResultParser(); return(parser.ParseResult(html)); }