public void HttpTest(List<StudyNo> lsn,string url)
        {
            try
            {
                var list_task = new List<Task<string>>();
                for (int i = 0; i < lsn.Count; i++)
                {
                    var temp_url = url + lsn[i];
                    var task = Task.Run(async () =>
                    {
                        System.Diagnostics.Debug.Print("temp_url=" + temp_url);
                        SenMessToTextBox("temp_url=" + temp_url);
                        var client = new System.Net.Http.HttpClient();
                        var html = await client.GetStringAsync(temp_url);
                        System.Diagnostics.Debug.Print("temp_url=" + temp_url + " is ok");
                        SenMessToTextBox("temp_url=" + temp_url + " is ok");
                        return lsn[i] + ":" + html;
                    });

                    list_task.Add(task);
                }

                Task.WaitAll(list_task.ToArray());

                foreach (var item in list_task)
                {
                    System.Diagnostics.Debug.Print(item.Result);
                    Write(item.Result);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                SenMessToTextBox(ex.InnerException+ "\r\n");
               // throw;
            }
           
        }
  Object ob = new object();
        /// <summary>
        /// 输出到log文件,格式 报告号:返回结果
        /// </summary>
        /// <param name="text"></param>
        public void Write(string text)
        {
            lock (ob)
            {
                FileStream fs = new FileStream(@"log.txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine(text);
                sw.Close();
                fs.Close();
            }
        }

 

posted on 2021-11-01 08:55  小石头的一天  阅读(379)  评论(0编辑  收藏  举报