下载文件时出现System.Net.WebException

时间:2016-04-23 21:03:21

标签: c# web-services file download httpwebrequest

我有一个我正在下载的mp3列表。下载了一些文件后,并非全部 - 大约5-7,我得到WebException。我做了一个堆栈跟踪,这就是结果。

Exception thrown: 'System.Net.WebException' in System.dll
Debug message: The operation has timed out
InnerEx:    at System.Net.HttpWebRequest.GetResponse()
       at iBlock.Main._InetGetHTMLSearch(String sArtist) in C:\Users\...\Main.cs:line 590

我的_ InetGetHTMLSearch看起来像这样

private void _InetGetHTMLSearch(string sArtist)
        {
            aLinks.Clear();
            if (AudioDumpQuery == string.Empty)
            {
                //return string.Empty;
            }
            string[] sStringArray;
            string sResearchURL = "http://www.audiodump.biz/music.html?" + AudioDumpQuery + sArtist.Replace(" ", "+");
            string aRet;
            HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(sResearchURL);
            webReq.UserAgent = "Mozilla / 5.0(Macintosh; Intel Mac OS X 10_9_3) AppleWebKit / 537.75.14(KHTML, like Gecko) Version / 7.0.3 Safari / 7046A194A";
            webReq.Referer = "http://www.audiodump.com/";
            webReq.Timeout = 5000;
            try
            {
                webReq.CookieContainer = new CookieContainer();
                webReq.Method = "GET";
                using (WebResponse response = webReq.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(stream);
                        aRet = reader.ReadToEnd();
                        //Console.WriteLine(aRet);
                        string[] aTable = _StringBetween(aRet, "<BR><table", "table><BR>", RegexOptions.Singleline);
                        if (aTable != null)
                        {
                            string[] aInfos = _StringBetween(aTable[0], ". <a href=\"", "<a href=\"");
                            if (aInfos != null)
                            {
                                for (int i = 0; i < aInfos.Length; i++)
                                {

                                    //do some magic here
                                }
                            }
                            else
                            {
                                //debug
                            }
                        }
                        else
                        {
                            //debug 2
                        }
                    }
                    response.Dispose();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Debug message: " + ex.Message + "InnerEx: " + ex.StackTrace);
                aLinks.Clear();
                return;
                //throw exception
            }
        }

这种方法的作用很简单。只需搜索sArtist

处的audiodump.com即可

我有一个计时器,运行速度非常快,每10毫秒。

private void MainTimer_Tick(object sender, EventArgs e)
        {
            _DoDownload(DoubleDimList[i][y], ref mp3ToPlay);
            if (muted) Mute(0);
            if (Downloading)
            {
                StatusLabel.Text = "Downloading: " + DoubleDimList[i][y];
            }
        }

现在,此计时器在全局范围内处理后台下载。 基本上开始整个过程​​的_ DoDownload方法看起来像这样

private void _DoDownload(string dArtist, ref string dPath)
        {
            if (!Contain && skip <= 3 && !Downloading)
            {
                try
                {
                    _InetGetHTMLSearch(dArtist);
                    if (aLinks.Count < 1)
                    {
                        //skip and return
                        Console.WriteLine("Skipping: " + dArtist);
                        IniWriteValue(_playlists[i], "Track " + y, dArtist + " -iBlockSkip");
                        y++;
                        return;
                    }
                    string path = mp3Path + "\\" + dArtist + ".mp3";
                    if (DownloadOne(aLinks[0], path, false))
                    {
                        hTimmer.Start();
                        Downloading = true;
                    }
                }
                catch (Exception Ex)
                {
                    MessageBox.Show("Download start error: " + Ex.Message);
                }
            }
            else if (Downloading)
            {
                try {
                    int actualBytes = strm.Read(barr, 0, arrSize);
                    fs.Write(barr, 0, actualBytes);
                    bytesCounter += actualBytes;
                    double percent = 0d;
                    if (fileLength > 0)
                        percent =
                            100.0d * bytesCounter /
                            (preloadedLength + fileLength);
                    label1.Text = Math.Round(percent) + "%";
                    if (Math.Round(percent) >= 100)
                    {
                        string path = mp3Path + "\\" + dArtist + ".mp3";
                        label1.Text = "";
                        dPath = path;
                        aLinks.Clear();
                        hTimmer.Stop();
                        hTimmer.Reset();
                        fs.Flush();
                        fs.Close();
                        lastArtistName = "N/A";
                        Downloading = false;
                        y++;
                        if (y >= DoubleDimList[i].Count)
                        {
                            i++;
                        }
                    }
                    if (Math.Round(percent) <= 1)
                    {
                        if (hTimmer.ElapsedMilliseconds >= 3000)
                        {
                            string path = mp3Path + "\\" + dArtist + ".mp3";
                            hTimmer.Stop();
                            hTimmer.Reset();
                            fs.Flush();
                            fs.Close();
                            System.IO.File.Delete(path);
                            Contain = false;
                            skip += 1;
                            Downloading = false;
                        }
                    } }
                catch(Exception Ex)
                {
                    MessageBox.Show("Downloading error: " + Ex.Message);
                }
            }
        }

现在一旦抛出异常,就会弄乱整个项目。正如您在上一个方法中看到的那样,如果_ InetGetHTMLSearch没有更新搜索(不返回任何内容),我正在跳过并转到下一个搜索。但是,每次下一次搜索都会抛出异常。我尝试在每次搜索中设置新的cookie但仍然无效。

任何解决方案如何避免此问题?

P.S。我不得不说,如果我将定时器的Interval更改为500毫秒,它将在抛出异常之前下载更多的mp3,但不是全部。

编辑:这里的问题很明显。请求超时,但即使我将其设置为Timeout.Infinite,它也会永远存在

0 个答案:

没有答案