首页 > Windows开发 > 详细

c# 异步请求demo

时间:2019-09-02 14:34:53      阅读:130      评论:0      收藏:0      [点我收藏+]

 

 public static async Task<List<Syncchip>> DownSyncchipList(int id)
        {
            using (var client = new HttpClient())
            {
                try
                {
                   
                    string url = ApiServer.url+"/school/door/syncchip";
                   
                    var content1 = new FormUrlEncodedContent(new Dictionary<string, string>()
                 {
                    //{"un","孙磊"},
                  
                    {"school_id", id.ToString()}
                 });
                    HttpResponseMessage response = await client.PostAsync(url, content1);

                    response.EnsureSuccessStatusCode();//用来抛异常的
                    string responseBody = await response.Content.ReadAsStringAsync();
                    ReponseSyncchip resModel = JsonConvert.DeserializeObject<ReponseSyncchip>(responseBody);
                    if (resModel.status == "200")
                    {
                        //修改该数据的IsUnLineSent的值
                        try
                        {
                            List<Syncchip> syncchip = resModel.data;

                            return syncchip;




                        }
                        catch (Exception se)
                        {
                            //MessageBox.Show(se.ToString());
                            //logger.Error("修改数据库失败" + se.Message);
                        }
                    }
                    else
                    {
                        //MessageBox.Show("失败");
                    }
                }

                catch (Exception ex)
                {
                   // MessageBox.Show("网路请求失败!");
                    return null;

                }
            }
            return null;
        }

 


            Stopwatch watch = new Stopwatch();         
            watch.Start();         
            Func<Task<List<Student>>> func_student = new Func<Task<List<Student>>>(() => ApiServer.DownStudentList(chool_id));
            student = await Task.Run(func_student);
            watch.Stop();

c# 异步请求demo

原文:https://www.cnblogs.com/ruiyuan/p/11445616.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!