C#程序在进行Web请求时停止处理

时间:2014-05-08 20:52:54

标签: c# webrequest

我对C#非常陌生,正在编写一个基本程序。目标是获取图像并将其保存到给定URL的磁盘。

这是我的代码基本上超时的地方:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m.Value.Trim()); // url string passed in from Regex function
HttpWebResponse resp = (HttpWebResponse) req.GetResponse(); // times out here

代码位于包含一个函数的类中,该函数按原样运行:

 String url; // passed in as a parameter
 String folder = @"C:\SMBC";
 // create directory if not exists to save comic
 if(!Directory.Exists(folder)) 
     Directory.CreateDirectory(folder);

     // visit the site and check for comics
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
     request.Method = "GET";
     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            while (reader.Peek() >= 0)
            {
                // if line contains "/comics/" then I'm in the right spot, we're at a URL now
                String line = reader.ReadLine();
                if (line.Contains("/comics/"))
                {
                    // pull out the address of the image // example result: http://www.smbc-comics.com/comics/20020905-2.gif
                    Regex linkParser = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    foreach (Match m in linkParser.Matches(line))
                    {
                        // new local file in folder, use original file name
                        String name = @"" + folder +"\\" + m.Value.Substring(m.Value.LastIndexOf("/") + 1);
                        Console.WriteLine(m.Value.Trim()); //http://www.smbc-comics.com/comics/20020905-2.gif
                        Uri uri = new Uri(m.Value.Trim());

                        Console.WriteLine("Making request"); // works 
                        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m.Value.Trim());
                        Console.WriteLine("getting response"); // works
                        HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
                        Console.WriteLine("opening stream"); // never shows
                        using (Stream inputStream = resp.GetResponseStream())
                        using (Stream outputStream = File.OpenWrite(name))
                        {
                            byte[] buffer = new byte[4096];
                            int bytesRead;
                            do
                            {
                                bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                                outputStream.Write(buffer, 0, bytesRead);
                            } while (bytesRead != 0);
                            //outputStream.Close();
                            //inputStream.Close();
                            //resp.Close();
                        }
                    }
                }
            }
        }

我在控制台中运行它,这就是我得到的:

enter image description here

过了一会儿,操作刚刚超时,我知道地址有效,因为我访问了它。

我有什么遗失的吗?

2 个答案:

答案 0 :(得分:2)

我认为你正在点击ServicePoint.ConnectionLimit。这可以通过在开始之前调整ServicePointManager.DefaultConnectionLimit来增加。不要忘记.Dispose可以在尽可能小的范围内处理的所有事情...... using陈述是好的。如果您没有关闭/处置WebRequest相关内容,即使您认为请求已完成,也可能会影响您的连接限制。

答案 1 :(得分:1)

作为一个新手,我不知道框架会限制任何给定时间允许的连接数。

因此,如果在连接循环中有循环,则必须覆盖它。

ServicePointManager.DefaultConnectionLimit = 4;