HttpClient 使用案例
get
HttpClient httpClient = new HttpClient(); var request = new HttpRequestMessage() { Content = null, Method = HttpMethod.Get, RequestUri = new Uri("https://oapi.dingtalk.com/gettoken?appkey=&appsecret=") }; var result = await httpClient.SendAsync(request);
form post
string url = "http://"; var values = new List<KeyValuePair<string, string>>(); values.Add(new KeyValuePair<string, string>("grant_type", "client_credentials")); values.Add(new KeyValuePair<string, string>("app_id", "")); values.Add(new KeyValuePair<string, string>("tenant_id", "")); values.Add(new KeyValuePair<string, string>("secret", "")); var formdata = new FormUrlEncodedContent(values); var result = await httpClient.PostAsync(url, formdata);
post 参数
var parameters = new Dictionary<string, string> { { "only_active", "false" }, { "param2", "2" } }; var request = new HttpRequestMessage() { Content = new StringContent(JsonConvert.SerializeObject(parameters)), Method = HttpMethod.Post, RequestUri = new Uri($"https://oapi.dingtalk.com/topapi/user/count?access_token={token}") };
设置heder:
httpClient.DefaultRequestHeaders.Add("X-PAAS-Tenant-ID", "204233"); httpClient.DefaultRequestHeaders.Add("Authrization", "sdfdsfdsfds");
获取返回值:
var r = httpClient.GetAsync("url").Result.Content.ReadAsStringAsync().Result;