标签:header cat success stat exception ast headers throw pre
直接干货:
public class HttpClientHelper { // <summary> /// post 请求 /// </summary> /// <param name="url"></param> /// <param name="xmlString"></param> /// <returns></returns> public static string PostXmlResponse(string url, string dataString) { HttpContent httpContent = new StringContent(dataString); httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/josn"); HttpClient httpClient = new HttpClient(); HttpResponseMessage res = httpClient.PostAsync(url, httpContent).Result; if (res.IsSuccessStatusCode) { Task<string> t = res.Content.ReadAsStringAsync(); return t.Result; } return string.Empty; } /// <summary> /// Get 请求 /// </summary> /// <param name="url"></param> /// <returns></returns> public static string GetResponse(string url) { HttpClient httpClient = new HttpClient(); HttpResponseMessage res = httpClient.GetAsync(url).Result; if (res.IsSuccessStatusCode) { Task<string> t = res.Content.ReadAsStringAsync(); return t.Result; } return string.Empty; } }
Post 或者如下调用:
try { System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient(); var values = new Dictionary<string, string> { { "cou_id", "A9E52C76-0CE7-4261-54C3-7EB04062F758" } }; var content = new System.Net.Http.FormUrlEncodedContent(values); var response = httpClient.PostAsync("http://localhost:8888/Api/Course/GetCourseStudent", content).Result; var responseString = response.Content.ReadAsStringAsync().Result; } catch (Exception ex) { throw new Exception(ex.Message); }
标签:header cat success stat exception ast headers throw pre
原文地址:https://www.cnblogs.com/Fengge518/p/12266911.html