.net Core System.Net.Http.HttpClient 发送post请求

                string url = "http://XX";

                string jsonContent = JsonConvert.SerializeObject(data);

                using (var client = new HttpClient())
                {
                    var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                    //上面代码使http Content-Type 为 application/json; charset=utf-8。如果希望Content-Type为application/json,可以使用下面两行代码
                    //content.Headers.Remove("Content-Type"); // "{application/json; charset=utf-8}"
                    //content.Headers.Add("Content-Type", "application/json");

                    client.DefaultRequestHeaders.Authorization =new AuthenticationHeaderValue("Bearer", encodedJwt);
                    string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
                }

你可能感兴趣的:(C#)