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


C# HttpClient.PutAsJsonAsync方法代码示例

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


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

示例1: Process

        private async static void Process()
        {
            //获取当前联系人列表
            HttpClient httpClient = new HttpClient();
            HttpResponseMessage response = await httpClient.GetAsync("http://localhost/selfhost/api/contacts");
            IEnumerable<Contact> contacts = await response.Content.ReadAsAsync<IEnumerable<Contact>>();
            Console.WriteLine("当前联系人列表:");
            ListContacts(contacts);

            //添加新的联系人
            Contact contact = new Contact { Name = "王五", PhoneNo = "0512-34567890", EmailAddress = "[email protected]" };
            await httpClient.PostAsJsonAsync<Contact>("http://localhost/selfhost/api/contacts", contact);
            Console.WriteLine("添加新联系人“王五”:");
            response = await httpClient.GetAsync("http://localhost/selfhost/api/contacts");
            contacts = await response.Content.ReadAsAsync<IEnumerable<Contact>>();
            ListContacts(contacts);

            //修改现有的某个联系人
            response = await httpClient.GetAsync("http://localhost/selfhost/api/contacts/001");
            contact = (await response.Content.ReadAsAsync<IEnumerable<Contact>>()).First();
            contact.Name = "赵六";
            contact.EmailAddress = "[email protected]";
            await httpClient.PutAsJsonAsync<Contact>("http://localhost/selfhost/api/contacts/001", contact);
            Console.WriteLine("修改联系人“001”信息:");
            response = await httpClient.GetAsync("http://localhost/selfhost/api/contacts");
            contacts = await response.Content.ReadAsAsync<IEnumerable<Contact>>();
            ListContacts(contacts);

            //删除现有的某个联系人
            await httpClient.DeleteAsync("http://localhost/selfhost/api/contacts/002");
            Console.WriteLine("删除联系人“002”:");
            response = await httpClient.GetAsync("http://localhost/selfhost/api/contacts");
            contacts = await response.Content.ReadAsAsync<IEnumerable<Contact>>();
            ListContacts(contacts);
        }
开发者ID:chenboyi081,项目名称:asp-net-web-api-2-samples,代码行数:35,代码来源:Program.cs

示例2: UpdateBalanz

 private async Task UpdateBalanz(string RekeningID, int nbalans)
 {
     System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
     using (var client = new HttpClient(new HttpClientHandler { UseProxy = false, ClientCertificateOptions = ClientCertificateOption.Automatic }))
     {
         client.BaseAddress = new Uri("https://hrsqlapp.tk");
         client.DefaultRequestHeaders.Accept.Clear();
         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
         HTTPget tmp = new HTTPget();
         Rekening uploadobject = tmp.getRekening(RekeningID);
         uploadobject.balans = nbalans;
         HttpResponseMessage response = await client.PutAsJsonAsync(String.Concat("/api/rekenings/", RekeningID), uploadobject).ConfigureAwait(false);
         if (response.IsSuccessStatusCode)
         {
             //Error.show("SUCCES");
         }
         else
         {
             //Error.show("FAIL");
         }
     }
 }
开发者ID:CHG1731,项目名称:ATM,代码行数:22,代码来源:MainProgram.cs


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