专注于分布式,性能优化,代码之美

使用httpClient 调用get,Post接口

1.httpClient 调用get接口

private async Task<IList<(int columnId, string columnName)>> GetFunction(int id)
{
var client = new HttpClient { BaseAddress = new Uri(BasicUrl) };
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await client.GetAsync($"/xxxx/xxxxxxx/Function?id={id}");
if (!response.IsSuccessStatusCode) throw new Exception("");
var json = await response.Content.ReadAsStringAsync();
var jObj = JsonConvert.DeserializeObject<JObject>(json);
return ((JArray) ((JObject)jObj["Data"])["ColumnConfigs"]).Select(i =>
{
var column = (JObject) i;
return (column["id"].Value<int>(), column["ColumnName"].Value<string>());
}).ToList();
}

 

2.httpClient 调用Post接口

 private async Task SaveData(int tableId, IList<List<(int columnId, string columnValue)>> rows )

{
var client = new HttpClient { BaseAddress = new Uri(BasicUrl) };
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var json = JsonConvert.SerializeObject(new
{
PkId = tableId.ToString(),
Data = data
});
var response = await client.PostAsync("/xxxx/xxx/Save", new StringContent(json, Encoding.UTF8, "application/json"));

var error = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode) throw new Exception("");
}

posted on 2019-08-20 10:32  xiaohouye  阅读(394)  评论(0编辑  收藏  举报

导航

今日之劳累是为了铸造明日之辉煌,不管年龄多少,都无法阻挡我对软件艺术的追求!