Richard Steiner
Richard Steiner

Reputation: 11

Deadlock using HttpWebRequest

I made a async method that is requesting 10 times an HTTP header using WebRequest. As long as the URL is invalid this my program is working fine. Is the URL valid, only two requests are sent.

To check this I made two Buttons, one to check the valid URL, one to check an invalid URL. If I use the valid URL, my counter is incremented exactly by 2, but only the first time. The funny thing is, that I can still press the button for the invalid URL and it's working as expected.

This is the cs file:

public partial class MainWindow : Window
{
    int counter = 0;

    private async Task DoWork(String url)
    {
        for (int i = 0; i < 10; i++)
        {
            HttpWebRequest request = WebRequest.CreateHttp(url);
            request.Method = "HEAD";
            request.Timeout = 100;

            HttpWebResponse response = null;

            try
            {
                response = (HttpWebResponse)await request.GetResponseAsync();
            }
            catch(Exception ex)
            {

            }

            counter++;
            Dispatcher.Invoke(() => label.Content = counter);
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        DoWork("http://www.google.ch");
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        DoWork("http://www.adhgfqliehfvufdigvhlnqaernglkjhr.ch");
    }
}

This is the xaml file

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label Name="label" Content="Label" HorizontalAlignment="Left" Margin="78,151,0,0" VerticalAlignment="Top"/>
        <Button Content="Valid URL" HorizontalAlignment="Left" Margin="64,71,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <Button Content="Invalid URL" HorizontalAlignment="Left" Margin="144,71,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
    </Grid>
</Window>

Can somebody explain this behavier?

Upvotes: 0

Views: 598

Answers (1)

Richard Steiner
Richard Steiner

Reputation: 11

The problem was not as expected a deadlock because of the async method. It is because I did not use dispose of the HttpWebResponse.

Here we found the hint to this problem HttpWebResponse get stuck while running in a loop

Also explained it is, why it was working exactly two times. The connection seems to stay open and there is a ConnectionLimit: System.Net.ServicePointManager.DefaultConnectionLimit

Adding dispose solved the problem:

            try
            {
                response = (HttpWebResponse)await request.GetResponseAsync();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
            }

Upvotes: 1

Related Questions