zoukankan      html  css  js  c++  java
  • HttpClient 基本使用

    Get 请求

        private async Task<List<Demo>> GetResource(HttpClient httpClient)
            {
                var response = await httpClient.GetAsync("api/demos");
                response.EnsureSuccessStatusCode();
    
                var content = await response.Content.ReadAsStringAsync();
                var demos = JsonConvert.DeserializeObject<List<Demo>>(content);
                return demos;
            }
    

    局部使用自定义请求头

        private async Task<List<Demo>> GetResourceThroughHttpRequestMessage(HttpClient httpClient)
            {
                var request = new HttpRequestMessage(HttpMethod.Get,"api/demos");
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
    
                var response = await httpClient.SendAsync(request);
                response.EnsureSuccessStatusCode();
    
                var content = await response.Content.ReadAsStringAsync();
                var serializer = new XmlSerializer(typeof(List<Demo>));
                var demos = (List<Demo>)serializer.Deserialize(new StringReader(content));
                return demos;
            }
    

    Post 请求

    简洁方式

        private async Task CreateResource(HttpClient httpClient)
            {
                var demoToCreate = new DemoForCreation()
                {
                    Title = "some title",
                    Description = "some description"
                };
    
                var response = await _httpClient.PostAsync(
                    "api/demos",
                    new StringContent(
                        JsonConvert.SerializeObject(demoToCreate),
                        Encoding.UTF8,
                        "application/json"));
                response.EnsureSuccessStatusCode();
    
                var content = await response.Content.ReadAsStringAsync();
                var demo = JsonConvert.DeserializeObject<Demo>(content);
            }
    

    HttpRequestMessage 方式

        private async Task CreateResource(HttpClient httpClient)
            {
                var demoToCreate = new DemoForCreation()
                {
                    Title = "some title",
                    Description = "some description"
                };
                var serializedDemoToCreate = JsonConvert.SerializeObject(demoToCreate);
    
                var request = new HttpRequestMessage(HttpMethod.Post,"api/demos");
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
    
                request.Content = new StringContent(serializedDemoToCreate);
                request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    
                var response = await httpClient.SendAsync(request);
                response.EnsureSuccessStatusCode();
                var content = await response.Content.ReadAsStringAsync();
                var serializer = new XmlSerializer(typeof(Demo));
                var demo = (Demo)serializer.Deserialize(new StringReader(content));
            }
    
  • 相关阅读:
    狗 日 的 360
    Django搭建简单的站点
    ZOJ 3675 Trim the Nails(bfs)
    Qt移动应用开发(二):使用动画框架
    SPOJ QTREE2 lct
    [Phonegap+Sencha Touch] 移动开发77 Cordova Hot Code Push插件实现自己主动更新App的Web内容
    Bitmap工具类BitmapHelper
    Material-design icon生成插件
    闲聊ROOT权限——ROOT权限的前世今生
    Java深入浅出系列(四)——深入剖析动态代理--从静态代理到动态代理的演化
  • 原文地址:https://www.cnblogs.com/Saints/p/12794408.html
Copyright © 2011-2022 走看看