Witam,
mam problem z funkcją responseStream.ReadAsync
otóż czasem nie zwraca ona wyniku tym samym program nigdy nie wyświetla "FINISH". W przypadku użycia ReadAsStringAsync()
zawsze działa prawidłowo - muszę pozostać przy ReadAsync
aby ograniczyć pobieranie zbyt dużych urli. Jakiś pomysł co zrobić aby funkcja działała tak jak powinna?
private async Task<bool> Open(string url)
{
try
{
var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(100));
using (var httpClient = new HttpClient())
{
using (var req = new HttpRequestMessage(HttpMethod.Get, new Uri(url)))
{
using (
HttpResponseMessage response =
await
httpClient.SendAsync(req, HttpCompletionOption.ResponseHeadersRead,
timeout.Token).ConfigureAwait(false))
{
if (response != null &&
(response.StatusCode == HttpStatusCode.OK ||
response.StatusCode == HttpStatusCode.NotFound))
{
using (
Stream responseStream =
await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
int read;
int offset = 0;
var rawResponse = new byte[8192];
while (
(read =
await
responseStream.ReadAsync(rawResponse, 0, 8192, timeout.Token)
.ConfigureAwait(false)) != 0)
{
offset += read;
if (offset > 1024000)
{
return false;
}
}
}
}
else
{
return false;
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return true;
}
private async void button4_Click(object sender, EventArgs e)
{
IEnumerable<string> lines = File.ReadLines("url.txt");
await
lines.ForEachAsync(200,
async line => { bool result = await Open(line).ConfigureAwait(false); });
Console.WriteLine("FINISH");
}