zoukankan      html  css  js  c++  java
  • c# 异步请求demo

     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();

  • 相关阅读:
    学会Git玩转Github笔记(一)——Github基本概念 & 仓库管理
    Github 入门基本操作
    Struts2拦截器浅析
    SSH(Struts2+Spring4+Hibernate4)框架教程之配置篇
    Spring 体系结构
    Ubuntu启动sshd服务
    jquery ajax/post 请求 案例
    Elasticsearch 配置
    Jenkins安装与配置
    解决Ubuntu环境变量错误导致无法正常登录  (command 'xxx' is available in bin ls)
  • 原文地址:https://www.cnblogs.com/ruiyuan/p/11445616.html
Copyright © 2011-2022 走看看