Skip to content

Instantly share code, notes, and snippets.

@Ershad95

Ershad95/file.cs Secret

Created January 28, 2023 05:02
Show Gist options
  • Save Ershad95/8cf7accbae02584637efb37d4fe30528 to your computer and use it in GitHub Desktop.
Save Ershad95/8cf7accbae02584637efb37d4fe30528 to your computer and use it in GitHub Desktop.
const int TARGET = 80;
var _httpClient = new HttpClient();
var _cancelationTokenSource = new CancellationTokenSource();
using (var response = await _httpClient.GetAsync(
"https://localhost:7284/customer",
HttpCompletionOption.ResponseHeadersRead,
_cancelationTokenSource.Token))
{
var stream = await response.Content.ReadAsStreamAsync(_cancelationTokenSource.Token);
var _jsonSerializerSettings = new JsonSerializerSettings();
var _serializer = Newtonsoft.Json.JsonSerializer.Create(_jsonSerializerSettings);
using TextReader textReader = new StreamReader(stream);
using JsonReader jsonReader = new JsonTextReader(textReader);
await using (stream.ConfigureAwait(false))
{
await jsonReader.ReadAsync(_cancelationTokenSource.Token).ConfigureAwait(false);
while (await jsonReader.ReadAsync(_cancelationTokenSource.Token).ConfigureAwait(false) &&
jsonReader.TokenType != JsonToken.EndArray)
{
Customer customer = _serializer!.Deserialize<Customer>(jsonReader);
if (customer.Id == TARGET)
{
Console.WriteLine(customer.Id + " : " + customer.Name);
_cancelationTokenSource.Cancel();
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment