ytshare
2015-10-19 01:42
采纳率: 50%
浏览 4.5k

【求助】webclient下载文件时 报System.Net.WebException: 操作超时”

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace SH600004Quickly
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient webClient = new WebClient();
            ServicePointManager.DefaultConnectionLimit = 100; 
            string Start = "2010年04月13日";
            string End = "2013年12月31日";
            DateTime dtStart = DateTime.Parse(Start);
            DateTime dtEnd = DateTime.Parse(End);
            int Days = (dtEnd - dtStart).Days + 1;//相隔天数
            string strPath = "F:\\gupiao111111\\";



            string Stockid = "600000";

            //string aFirstName = args[0].Substring(args[0].LastIndexOf("\\") + 1, (args[0].LastIndexOf(".") - args[0].LastIndexOf("\\") - 1));
            for (int i = 0; i < Days; i++)
            {

                string url = null;
                string sDay = dtStart.AddDays(i).ToString("yyyy-MM-dd"); //每一天

                url = "http://market.finance.sina.com.cn/downxls.php?date=" + sDay + "&symbol=sh" + Stockid;

                Console.WriteLine(url);

                DirectoryInfo stockDir = Directory.CreateDirectory(strPath + sDay);
                string dataDir = stockDir.FullName + "\\data.txt";
                webClient.DownloadFile(url, dataDir);

                //webClient.Dispose();

                //WriteStr2FileEnd("C:\\Users\\T.Yang\\Desktop\\errorLog.txt", sDay + " " + aFirstName);
                //strPath = "F:\\gupiao111111\\";

            }

            //将每个文件夹里的文件用bat处理
        }

        public static void WriteStr2FileEnd(String filename, String content)//自动换行写入
        {
            FileStream fsLineNo = new FileStream(@filename, System.IO.FileMode.OpenOrCreate, FileAccess.Write);
            fsLineNo.Seek(fsLineNo.Length, SeekOrigin.Begin);
            StreamWriter swLineNo = new StreamWriter(fsLineNo);
            swLineNo.WriteLine(content);
            swLineNo.Close();
            fsLineNo.Close();
        }

    }
}

每次下载几个文件后,就卡在那不动了,怎么办?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 这个还能被注册? 2015-10-19 09:22

    WebClient webClient = new WebClient(); 把这个放FOR循环里面试试?

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题