V
V
Victor Nasonov2018-02-28 13:09:53
C++ / C#
Victor Nasonov, 2018-02-28 13:09:53

How to reconnect on webclient error?

this is how I do reconnect now, but what if it fails to connect and download data twice, is there a solution?

Stream data;
StreamReader reader;
try
{
    data = client.OpenRead("https://api.vk.com/method/groups.getById?" + window.Token + "&group_ids=" + groups_ids + "&v=" + window.version);
    reader = new StreamReader(data);
}
catch (Exception)
{
    data = client.OpenRead("https://api.vk.com/method/groups.getById?" + window.Token + "&group_ids=" + groups_ids + "&v=" + window.version);
    reader = new StreamReader(data);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tex0, 2018-02-28
@kvonosan

the easiest:

Stream data;
StreamReader reader;
while(true) // тут в условие можете поставить счетчик попыток реконнекта и соответственно оформить его в теле цикла
{
    try
    {
        data = client.OpenRead("https://api.vk.com/method/groups.getById?" + window.Token + "&group_ids=" + groups_ids + "&v=" + window.version);
        reader = new StreamReader(data);
        break;
    }
    catch (Exception)
    {
        // тут можно писать ошибки в лог
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question