WebRequest和HttpClient的Get、post方式解析:
namespace Fractalist.Benz.Controllers { public class ResolveResultController : Controller { public void WebRequestPost() { HttpWebResponse response = null; try { string url = "http://localhost:8022/ResolveResult/PostTest"; dynamic expando = new System.Dynamic.ExpandoObject(); expando.name = "gw"; var jsonInfo = JsonConvert.SerializeObject(expando); byte[] byteArray = Encoding.UTF8.GetBytes(jsonInfo); //string result = new HttpClient().GetStringAsync(url).Result; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = byteArray.Length; Stream newStream = request.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length); //写入参数 newStream.Close(); response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { response = (HttpWebResponse)ex.Response; } StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string textResponse = sr.ReadToEnd(); // 返回的数据 JsonResultInfo deinfo = JsonConvert.DeserializeObject<JsonResultInfo>(textResponse); var results = deinfo.Result; var message = deinfo.message; } public void HttpClientGet() { string url = "http://localhost:8022/ResolveResult/GetTest"; //封装请求参数 Task<string> response = new HttpClient().GetStringAsync(url + "?name=gw"); string result = response.Result; JsonResultInfo deinfo = JsonConvert.DeserializeObject<JsonResultInfo>(result); var results = deinfo.Result; var message = deinfo.message; } public async Task<ActionResult> HttpClientPost() { string url = "http://localhost:8022/ResolveResult/PostTest"; var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; using (var client = new HttpClient(handler)) { //第一种方式 var content = new FormUrlEncodedContent(new Dictionary<string, string>() { {"name","glw"} }); //第二种方式 //string jsonData = ReturnJson("name", "glw"); //var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); //封装请求参数 var response = await client.PostAsync(url, content); string result = await response.Content.ReadAsStringAsync(); JsonResultInfo deinfo = JsonConvert.DeserializeObject<JsonResultInfo>(result); var results = deinfo.Result; var message = deinfo.message; return Content(message); } } [AllowAnonymous] [HttpPost] public JsonResult PostTest(string name) { string.Format(@"【class】:ResolveResultController,【method】:Test,【message】:name---{0}" , name).LogInfo(); return Json(new { Result = false, Message = name + "我是好人也是个坏人" }); } [AllowAnonymous] public JsonResult GetTest(string name) { return Json(new { Result = false, Message = name + "我是好人也是个坏人" }, JsonRequestBehavior.AllowGet); } /// <summary> /// 组装请求json /// </summary> /// <param name="code"></param> /// <param name="value"></param> /// <returns></returns> static string ReturnJson(string code, string value) { Hashtable hastable = new Hashtable(); hastable.Add(code, value); return Newtonsoft.Json.JsonConvert.SerializeObject(hastable); } } }
时间: 2024-10-06 00:16:10