请求后台,有很多方式,常见的有WebRequest、HttpClient。以下mark一上~
WebRequest方式
1. Get
1 public static async Task<string> RequestDataAsync(string requestUrl) 2 { 3 WebRequest translationWebRequest = WebRequest.Create(requestUrl); 4 5 var response = await translationWebRequest.GetResponseAsync(); 6 7 using (Stream stream = response.GetResponseStream()) 8 { 9 using (StreamReader reader = new StreamReader(stream ?? throw new InvalidOperationException(), 10 Encoding.GetEncoding("utf-8"))) 11 { 12 string result = reader.ReadToEnd(); 13 var decodeResult = Unicode2String(result); 14 return decodeResult; 15 } 16 } 17 }
2. Post
1 /// <summary> 2 /// Post using WebRequest 3 /// </summary> 4 /// <param name="requestUrl"></param> 5 /// <param name="jsonData"></param> 6 /// <returns></returns> 7 public static async Task<string> PostDataAsync(string requestUrl,string jsonData) 8 { 9 WebRequest translationWebRequest = WebRequest.Create(requestUrl); 10 translationWebRequest.Method = "post"; 11 translationWebRequest.ContentType = "application/json"; 12 13 byte[] postdatabyte = Encoding.UTF8.GetBytes(jsonData); 14 translationWebRequest.ContentLength = postdatabyte.Length; 15 using (Stream postStream= translationWebRequest.GetRequestStream()) 16 { 17 postStream.Write(postdatabyte, 0, postdatabyte.Length); 18 } 19 20 var response = await translationWebRequest.GetResponseAsync(); 21 22 using (Stream responseStream = response.GetResponseStream()) 23 { 24 using (StreamReader reader = new StreamReader(responseStream ?? throw new InvalidOperationException(), 25 Encoding.GetEncoding("utf-8"))) 26 { 27 string result = reader.ReadToEnd(); 28 var decodeResult = Unicode2String(result); 29 return decodeResult; 30 } 31 } 32 }
HttpClient方式
1 /// <summary> 2 /// Post using HttpClient 3 /// </summary> 4 /// <param name="requestUrl"></param> 5 /// <param name="jsonData"></param> 6 /// <returns></returns> 7 public static async Task<string> PostDataUsingHttpAsync(string requestUrl, string jsonData) 8 { 9 HttpContent httpContent = new StringContent(jsonData); 10 httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 11 HttpClient httpClient = new HttpClient(); 12 HttpResponseMessage response = httpClient.PostAsync(requestUrl, httpContent).Result; 13 if (response.IsSuccessStatusCode) 14 { 15 Task<string> t = response.Content.ReadAsStringAsync(); 16 return t.Result; 17 } 18 return string.Empty; 19 }
拿走,不谢~
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public class WebRequestBase 2 { 3 public static async Task<string> RequestDataAsync(string requestUrl) 4 { 5 WebRequest translationWebRequest = WebRequest.Create(requestUrl); 6 7 var response = await translationWebRequest.GetResponseAsync(); 8 9 using (Stream stream = response.GetResponseStream()) 10 { 11 using (StreamReader reader = new StreamReader(stream ?? throw new InvalidOperationException(), 12 Encoding.GetEncoding("utf-8"))) 13 { 14 string result = reader.ReadToEnd(); 15 var decodeResult = Unicode2String(result); 16 return decodeResult; 17 } 18 } 19 } 20 21 /// <summary> 22 /// Post using WebRequest 23 /// </summary> 24 /// <param name="requestUrl"></param> 25 /// <param name="jsonData"></param> 26 /// <returns></returns> 27 public static async Task<string> PostDataAsync(string requestUrl, string jsonData) 28 { 29 WebRequest translationWebRequest = WebRequest.Create(requestUrl); 30 translationWebRequest.Method = "post"; 31 translationWebRequest.ContentType = "application/json"; 32 33 byte[] postdatabyte = Encoding.UTF8.GetBytes(jsonData); 34 translationWebRequest.ContentLength = postdatabyte.Length; 35 using (Stream postStream = translationWebRequest.GetRequestStream()) 36 { 37 postStream.Write(postdatabyte, 0, postdatabyte.Length); 38 } 39 40 var response = await translationWebRequest.GetResponseAsync(); 41 42 using (Stream responseStream = response.GetResponseStream()) 43 { 44 using (StreamReader reader = new StreamReader(responseStream ?? throw new InvalidOperationException(), 45 Encoding.GetEncoding("utf-8"))) 46 { 47 string result = reader.ReadToEnd(); 48 var decodeResult = Unicode2String(result); 49 return decodeResult; 50 } 51 } 52 } 53 54 /// <summary> 55 /// Post using HttpClient 56 /// </summary> 57 /// <param name="requestUrl"></param> 58 /// <param name="jsonData"></param> 59 /// <returns></returns> 60 public static async Task<string> PostDataUsingHttpAsync(string requestUrl, string jsonData) 61 { 62 HttpContent httpContent = new StringContent(jsonData); 63 httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 64 HttpClient httpClient = new HttpClient(); 65 HttpResponseMessage response = httpClient.PostAsync(requestUrl, httpContent).Result; 66 if (response.IsSuccessStatusCode) 67 { 68 Task<string> t = response.Content.ReadAsStringAsync(); 69 return t.Result; 70 } 71 return string.Empty; 72 } 73 74 /// <summary> 75 /// Unicode转字符串 76 /// </summary> 77 /// <param name="source">经过Unicode编码的字符串</param> 78 /// <returns>正常字符串</returns> 79 protected static string Unicode2String(string source) 80 { 81 return new Regex(@"\\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace( 82 source, x => string.Empty + Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16))); 83 } 84 }