WebClient Retry

廉价感情. 提交于 2019-12-24 03:29:30

问题


Is it possible to retry a webclient request? On the odd occasion my application will throw an error when attempting to connect to an xml web service but if I retry, it works OK. I'd like it to retry 2 times before throwing an error unless someone has a better solution :)

private void ApplicationBarLogin_Click(object sender, EventArgs e)
        {
            settings.UsernameSetting = Username.Text;
            if (RememberPassword.IsChecked == true)
            {
                settings.PasswordSetting = Password.Password;
                settings.RememberPasswordSetting = true;
            }
            else
            {
                settings.RememberPasswordSetting = false;
            }

            WebClient internode = new WebClient();

            internode.Credentials = new NetworkCredential(settings.UsernameSetting, settings.PasswordSetting);
            internode.DownloadStringCompleted += new DownloadStringCompletedEventHandler(internode_DownloadStringCompleted);
            internode.DownloadStringAsync(new Uri("https://customer-webtools-api.internode.on.net/api/v1.5/"));
        }

        public void internode_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else
            {
                MessageBox.Show("Authentication successfull.");
            }
        }

回答1:


If you get a failure, you could re-issue the request. By keeping count of the number of times you re-issue the request you can determine when to show the user an error. Here is a quick modification to your code to demonstrate what I mean.

private void ApplicationBarLogin_Click(object sender, EventArgs e)
{
    settings.UsernameSetting = Username.Text;
    if (RememberPassword.IsChecked == true)
    {
        settings.PasswordSetting = Password.Password;
        settings.RememberPasswordSetting = true;
    }
    else
    {
        settings.RememberPasswordSetting = false;
    }

    WebClient internode = new WebClient();

    internode.Credentials = new NetworkCredential(settings.UsernameSetting, settings.PasswordSetting);
    internode.DownloadStringCompleted += new DownloadStringCompletedEventHandler(internode_DownloadStringCompleted);
    internode.DownloadStringAsync(new Uri("https://customer-webtools-api.internode.on.net/api/v1.5/"));
}

private int _retryCount = 0;

public void internode_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error != null)
    {
        _retryCount++;
        if (_retryCount < 3)
        {
            WebClient internode = (WebClient)sender;
            internode.DownloadStringAsync(new Uri("https://customer-webtools-api.internode.on.net/api/v1.5/"));
        }
        else
        {
            _retryCount = 0;
            MessageBox.Show(e.Error.Message);
        }
    }
    else
    {
        _retryCount = 0;
        MessageBox.Show("Authentication successfull.");
    }
}



回答2:


WebClient doesn't have any built in retry functionality.

You should look to build the retry logic yourself before, probably, informing the user of the problem.



来源:https://stackoverflow.com/questions/7527682/webclient-retry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!