Mikkel Refsgaard
Mikkel Refsgaard

Reputation: 111

Checks for webserver response

I'm making this folder scanner on a website, but after 2 successful urls it will crash and i got no idea why.

if (File.Exists(filePath))
{
    StreamReader file = null;

    file = new StreamReader(filePath);

    while ((line = file.ReadLine()) != null)
    {

        var url = new Uri(txtUrl.Text + line);
        try
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.AllowAutoRedirect = true;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Win32)";
            var response = (HttpWebResponse)request.GetResponse();

            //Directory.ListView.Items.Add(url.ToString());
            MessageBox.Show(url.ToString() + "Success");
        }
        catch (Exception err)
        {
            MessageBox.Show(url.ToString() + " fail: " + err.Message);
        }

    }
    if (file != null)
        file.Close();
    MessageBox.Show("done;");
}

Upvotes: 1

Views: 326

Answers (1)

dr. evil
dr. evil

Reputation: 27265

You need to close your HTTPWebResponse object,

response.Close()

Upvotes: 1

Related Questions