检查Web服务器响应

时间:2011-02-17 23:50:19

标签: c# .net httpwebrequest

我在网站上制作这个文件夹扫描仪, 但是在2个成功的网址之后它会崩溃,我不明白为什么。

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;");
}

1 个答案:

答案 0 :(得分:1)

您需要关闭HTTPWebResponse对象

response.Close()