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


C# HttpClient.SendAsJsonAsync方法代码示例

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


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

示例1: Create

		public async Task<string> Create(string name)
		{
			try
			{
				using (HttpClient httpClient = new HttpClient())
				{
					httpClient.BaseAddress = new Uri(_resource);
					httpClient.Timeout = new TimeSpan(0, 2, 0);
					httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
					httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
					httpClient.DefaultRequestHeaders.Accept.Add(
						new MediaTypeWithQualityHeaderValue("application/json"));
					httpClient.DefaultRequestHeaders.Authorization =
						new AuthenticationHeaderValue("Bearer", _accessToken);

					//Create
					JObject newAccount = new JObject
					{
						{"name", name},
						{"telephone1", "111-888-7777"}
					};

					//The URL will change in 2016 to include the API version - api/data/v8.0/accounts
					HttpResponseMessage createResponse =
						await httpClient.SendAsJsonAsync(HttpMethod.Post, "api/data/accounts", newAccount);

					if (!createResponse.IsSuccessStatusCode)
						return null;

					string accountUri = createResponse.Headers.GetValues("OData-EntityId").FirstOrDefault();
					return accountUri != null ?
						Guid.Parse(accountUri.Split('(', ')')[1]).ToString() :
						null;
				}
			}
			catch (Exception ex)
			{
				ShowException(ex);
				return null;
			}
		}
开发者ID:jlattimer,项目名称:CrmWebApiWinUniv,代码行数:41,代码来源:MainPage.xaml.cs

示例2: Update

		public async Task<string> Update(string accountId)
		{
			try
			{
				using (HttpClient httpClient = new HttpClient())
				{
					httpClient.BaseAddress = new Uri(_resource);
					httpClient.Timeout = new TimeSpan(0, 2, 0);
					httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
					httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
					httpClient.DefaultRequestHeaders.Accept.Add(
						new MediaTypeWithQualityHeaderValue("application/json"));
					httpClient.DefaultRequestHeaders.Authorization =
						new AuthenticationHeaderValue("Bearer", _accessToken);

					//Update 
					JObject account = new JObject
					{
						{"websiteurl", "http://www.microsoft.com"}
					};

					//The URL will change in 2016 to include the API version - api/data/v8.0/accounts
					HttpResponseMessage updateResponse =
						await httpClient.SendAsJsonAsync(new HttpMethod("PATCH"), "api/data/accounts(" + accountId + ")", account);
					return updateResponse.IsSuccessStatusCode ? accountId : null;
				}
			}
			catch (Exception ex)
			{
				ShowException(ex);
				return null;
			}
		}
开发者ID:jlattimer,项目名称:CrmWebApiWinUniv,代码行数:33,代码来源:MainPage.xaml.cs

示例3: CloseTask

		private async void CloseTask(string taskId)
		{
			JObject task = new JObject { { "statecode", 1 }, { "statuscode", 5 } };

			using (HttpClient httpClient = new HttpClient())
			{
				httpClient.BaseAddress = new Uri(ServiceUrl);
				httpClient.Timeout = new TimeSpan(0, 2, 0);
				httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
				httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
				httpClient.DefaultRequestHeaders.Accept.Add(
					new MediaTypeWithQualityHeaderValue("application/json"));
				httpClient.DefaultRequestHeaders.Authorization =
					new AuthenticationHeaderValue("Bearer", NSUserDefaults.StandardUserDefaults.StringForKey("AccessToken"));

				HttpResponseMessage closeResponse =
						await httpClient.SendAsJsonAsync(new HttpMethod("PATCH"), "api/data/tasks(" + taskId + ")", task);

				if (!closeResponse.IsSuccessStatusCode)
					return;
			}
		}
开发者ID:jlattimer,项目名称:CrmWatchKit,代码行数:22,代码来源:AppDelegate.cs


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