当前位置: 首页>>代码示例>>C#>>正文


C# HttpClient.RequestAsync方法代码示例

本文整理汇总了C#中System.Net.Http.HttpClient.RequestAsync方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.RequestAsync方法的具体用法?C# HttpClient.RequestAsync怎么用?C# HttpClient.RequestAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Http.HttpClient的用法示例。


在下文中一共展示了HttpClient.RequestAsync方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: LoginAsync

        public async Task<bool> LoginAsync(string uName, string pwd, bool fromCache = false)
        {
            using (var client = new HttpClient(new NativeMessageHandler()))
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, Configuration.Routes.LoginUrl))
                {
                    request.Content = new JsonContent(JsonConvert.SerializeObject(new LoginCredentials(uName, pwd)));

                    try
                    {
                        var resp = await client.RequestAsync<LoginResponse>(request);
                        if (resp.Data == null || !resp.Data.Success)
                        {
                            throw new Exception("Login Failed" + ((resp.Data != null && !string.IsNullOrEmpty(resp.Data.ErrorMessage)) ? "\n Error: " + resp.Data.ErrorMessage : string.Empty));
                        }
                        LoggedinUser = resp.Data.User;
                        await BlobCache.Secure.SaveLogin(uName, pwd);
                    }
                    catch (NetworkException n)
                    {
                        throw new Exception("Login server is unreachable", n);
                    }

                    return true;
                }
            }
        }
开发者ID:kmjonmastro,项目名称:LunchPacMobile,代码行数:27,代码来源:LoginManager.cs

示例2: FetchRestaurants

 async Task<List<Restaurant>> FetchRestaurants()
 {
     using (var client = new HttpClient(new NativeMessageHandler()))
     {
         using (var req = new HttpRequestMessage(HttpMethod.Get, Configuration.Routes.RestaurantsUrl + "?date=" + WebUtility.UrlEncode(DateTime.UtcNow.ToISOString())))
         {
             try
             {
                 var res = await client.RequestAsync<List<Restaurant>>(req);
                 return res.Data;
             }
             catch (Exception e)
             {
                 throw new Exception("Failed to retrive list of restaurants", e);
             }
         }
     }
 }
开发者ID:kmjonmastro,项目名称:LunchPacMobile,代码行数:18,代码来源:DomainManager.cs

示例3: FetchOrderingStatus

        public async Task<OrderStatus> FetchOrderingStatus()
        {
            using (var client = new HttpClient(new NativeMessageHandler()))
            {
                using (var req = new HttpRequestMessage(HttpMethod.Get, Configuration.Routes.OrderingStatus + "?date=" + WebUtility.UrlEncode(DateTime.UtcNow.ToISOString())))
                {
                    try
                    {
                        var resp = await client.RequestAsync<OrderStatus>(req);

                        OrderingStatusOpen = !resp.Data.Closed;

                        return resp.Data;
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Failed to retrieve odering status:" + e.FullMessage(), e);
                    }
                }
            }
        }
开发者ID:kmjonmastro,项目名称:LunchPacMobile,代码行数:21,代码来源:DomainManager.cs

示例4: UpsertOrder

        public async Task UpsertOrder(Order order)
        {
            if (string.IsNullOrEmpty(order.OrderItem) && string.IsNullOrEmpty(order.Soup) && string.IsNullOrEmpty(order.OrderComments))
            {
                throw new Exception("You must fill at least one field.");
            }
                
            await FetchOrderingStatus();

            if (!OrderingStatusOpen)
            {
                throw new Exception("Lunch ordering is closed :(.\nPlease contact the responsible team directly.");
            }

            order.AddDate = DateTime.UtcNow;
            order.UserId = LoginManager.LoggedinUser.UserId;

            using (var client = new HttpClient(new NativeMessageHandler()))
            {
                var method = order.OrderId.HasValue ? HttpMethod.Put : HttpMethod.Post;
                using (var req = new HttpRequestMessage(method, Configuration.Routes.Order))
                {
                    try
                    {
                        var history = await GetHistory();
                        var json = JsonConvert.SerializeObject(order);
                        req.Content = new JsonContent(json);

                        if (order.OrderId.HasValue)
                        {
                            await client.RequestAsync(req);
                            history[order.RestaurantId].RemoveAll(o => o.OrderId == order.OrderId);
                        }
                        else
                        {
                            var res = await client.RequestAsync<int>(req);
                            order.OrderId = res.Data;
                        }

                        history[order.RestaurantId].Add(order);
                        var sorted = history[order.RestaurantId].OrderByDescending(o => o.AddDate).ToList();
                        history[order.RestaurantId] = sorted;
                        await BlobCache.InMemory.InsertObject<Dictionary<int, List<Order>>>(Configuration.CacheKeys.OrderHistory, history);
                    }
                    catch (NetworkException e)
                    {
                        throw new Exception("You must be online.", e);
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Unable to create order. Error:" + e.FullMessage(), e);
                    }
                }
            }
        }
开发者ID:kmjonmastro,项目名称:LunchPacMobile,代码行数:55,代码来源:DomainManager.cs

示例5: DeleteOrder

        public async Task DeleteOrder(Order order)
        {
            await FetchOrderingStatus();

            if (!OrderingStatusOpen)
            {
                throw new Exception("Lunch ordering is closed :(.\nPlease contact the responsible team directly.");
            }
//            #if DEBUG
//            throw new Exception("Not Available in Debug Mode");
//            #endif

            using (var client = new HttpClient(new NativeMessageHandler()))
            {
                using (var req = new HttpRequestMessage(HttpMethod.Delete, Configuration.Routes.Order + "/?orderid=" + order.OrderId))
                {
                    try
                    {
                        await client.RequestAsync(req);
                      
                        var history = await GetHistory();
                        history[order.RestaurantId].RemoveAll(o => o.OrderId == order.OrderId);
                        await BlobCache.InMemory.InsertObject<Dictionary<int, List<Order>>>(Configuration.CacheKeys.OrderHistory, history);
                    }
                    catch (NetworkException e)
                    {
                        throw new Exception("You must be online.", e);
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Failed to delete order. Error:" + e.FullMessage(), e);
                    }
                }
            }
        }
开发者ID:kmjonmastro,项目名称:LunchPacMobile,代码行数:35,代码来源:DomainManager.cs

示例6: FetchHistoryAsync

 static async Task<List<Order>> FetchHistoryAsync(Restaurant rest)
 {
     using (var client = new HttpClient(new NativeMessageHandler()))
     {
         using (var req = new HttpRequestMessage(HttpMethod.Get, Configuration.Routes.OrderhistoryUrl + "?userid=" + LoginManager.LoggedinUser.UserId + "&restaurantId=" + rest.RestaurantId))
         {
             try
             {
                 var res = await client.RequestAsync<List<Order>>(req);
                 return res.Data;
             }
             catch (Exception e)
             {
                 throw new Exception("Unable to fetch history Error:" + e.FullMessage(), e);
             }
         }
     }
 }
开发者ID:kmjonmastro,项目名称:LunchPacMobile,代码行数:18,代码来源:DomainManager.cs


注:本文中的System.Net.Http.HttpClient.RequestAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。