问题描述
- 【求助】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(); } } }
每次下载几个文件后,就卡在那不动了,怎么办?
解决方案
WebClient webClient = new WebClient(); 把这个放FOR循环里面试试?
时间: 2024-09-18 09:31:50