引用 Newtonsoft.Json
// Post请求 public string PostResponse(string url,string postData,out string statusCode) { string result = string.Empty; //设置Http的正文 HttpContent httpContent = new StringContent(postData); //设置Http的内容标头 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); //设置Http的内容标头的字符 httpContent.Headers.ContentType.CharSet = "utf-8"; using(HttpClient httpClient=new HttpClient()) { //异步Post HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; //输出Http响应状态码 statusCode = response.StatusCode.ToString(); //确保Http响应成功 if (response.IsSuccessStatusCode) { //异步读取json result = response.Content.ReadAsStringAsync().Result; } } return result; } // 泛型:Post请求 public T PostResponse<T>(string url,string postData) where T:class,new() { T result = default(T); HttpContent httpContent = new StringContent(postData); httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); httpContent.Headers.ContentType.CharSet = "utf-8"; using(HttpClient httpClient=new HttpClient()) { HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; if (response.IsSuccessStatusCode) { Task<string> t = response.Content.ReadAsStringAsync(); string s = t.Result; //Newtonsoft.Json string json = JsonConvert.DeserializeObject(s).ToString(); result = JsonConvert.DeserializeObject<T>(json); } } return result; }