zoukankan      html  css  js  c++  java
  • 解析Action返回的Json数据

    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);
            }
        }
    }
  • 相关阅读:
    理解JavaScript Call()函数原理。
    数据结构水题大赛官方题解#3 XXX的stl
    数据结构水题大赛官方题解#2 XXX的脑白金
    数据结构水题大赛官方题解#1 XXX的面试
    P3390 矩阵快速幂
    2020.6.8 T1 棋子移动
    U68862 奶牛滑迷宫
    5月18日考试错误题目走迷宫
    最小生成树两种算法详解
    p1220关路灯(小优化)
  • 原文地址:https://www.cnblogs.com/xiaoweigogo/p/7808455.html
Copyright © 2011-2022 走看看