本文整理汇总了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;
}
}
示例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;
}
}
示例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;
}
}