private static void DownloadFile(object odata) { ParamData data = odata as ParamData; var code = data.code; var url = data.url; var date = data.date; var filename = string.Format("D:\\tradedata\\{0}\\{1}.xls", date, code); var fileinfo = new FileInfo(filename); if (!Directory.Exists(fileinfo.DirectoryName)) { Directory.CreateDirectory(fileinfo.DirectoryName); } try { System.Net.HttpWebRequest myrq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); myrq.UserAgent = useragent[random.Next(5)]; myrq.Timeout = 2000; System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)myrq.GetResponse(); if (myrp.StatusCode == System.Net.HttpStatusCode.OK) { long totalBytes = myrp.ContentLength; System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create); long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { totalDownloadedByte = osize + totalDownloadedByte; so.Write(by, 0, osize); osize = st.Read(by, 0, (int)by.Length); } so.Close(); st.Close(); } else { lock (lockobj) { File.AppendAllText(logfilepath, "code:[" + code + "] 超时"); } } } catch (Exception exc) { lock (lockobj) { File.AppendAllText(logfilepath, "code:[" + code + "] " + exc.ToString()); } } }
public static void GetDailyData(DateTime dt) { GetCodeList(stockpath); var formatdate = dt.ToString("yyyyMMdd"); var formatcode = string.Empty; foreach (string code in codelist) { if (code.StartsWith("6")) { formatcode = "sh" + code; } else { formatcode = "sz" + code; } ParamData data = new ParamData(string.Format(qqurl, formatcode, formatdate), code, formatdate); ThreadPool.QueueUserWorkItem(DownloadFile, data); } }