服务端接受到的post数据应该怎么解析啊

a3723683 2019-09-25 12:00:54



通过hp获取到了post过来的数据 怎么解析这个数据 比如想获得这个文件流或者access_token 值 等等

求代码 找了一上午了都没找到
...全文
749 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_45345524 2020-07-06
  • 打赏
  • 举报
回复
怎么解析http服务器端收到的post类型的数据啊,求大佬赐教
a3723683 2019-09-26
  • 打赏
  • 举报
回复
搞定了感谢大伙
  • 打赏
  • 举报
回复
然后其实还有一些post并没有支持,你可以根据自己的需求自己增加,我这里只支持了get post body 和post过来直接是json的数据
  • 打赏
  • 举报
回复
哦对我顺便加了个token 你不需要自己把不需要的代码拿出去
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;

//自己写namespace
        /// <summary>
        /// 通过Action中的Request获取全部请求到Dictionary<string, object>
        /// </summary>
        /// <param name="requestInAction">Request对象</param>
        /// <returns>全部请求Map  Dictionary<string, object></returns>
        public static Dictionary<string, object> AllRequests(HttpRequestMessage requestInAction)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();

            foreach (var item in requestInAction.Headers)
            {
                if (item.Key.ToLower() == "token")
                {
                    result.Add("token", item.Value.FirstOrDefault().ToString());
                }
            }

            var method = requestInAction.Method;
            foreach (var item in requestInAction.GetQueryNameValuePairs())
            {
                if (!result.ContainsKey(item.Key.ToLower()))
                {
                    result.Add(item.Key.ToLower(), item.Value);
                }
            }

            if (requestInAction.Content.IsFormData())
            {
                var context = (System.Web.HttpContextBase)requestInAction.Properties["MS_HttpContext"];//获取传统context     
                var request = context.Request;//定义传统request对象
                foreach (var item in request.Form.AllKeys)
                {
                    if (!result.ContainsKey(item.ToLower()))
                    {
                        result.Add(item.ToLower(), request.Form[item]);
                    }
                }
            }
            else
            {
                try
                {
                    var resultString = requestInAction.Content.ReadAsStringAsync().Result;
                    Newtonsoft.Json.Linq.JObject @object = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(resultString);
                    foreach (var item in @object)
                    {
                        if (!result.ContainsKey(item.Key.ToLower()))
                        {
                            result.Add(item.Key.ToLower(), item.Value);
                        }
                    }

                }
                catch (Exception ex)
                {
                    result.Add("JsonErrorAt", $"{ex.HResult}-{ex.Message}");
                    //throw;
                }
            }

            return result;
        }
用这个方法可以吧全部的请求都读取到一个字典表里面。
qq_40085310 2019-09-25
  • 打赏
  • 举报
回复
Stream s = HttpContext.Current.Request.InputStream; byte[] b = new byte[s.Length]; s.Read(b, 0, (int)s.Length); string Sub= Encoding.UTF8.GetString(b); JObject jo = (JObject)JsonConvert.DeserializeObject(Sub); string openId = string.IsNullOrEmpty(jo["OpenId"].ToString()) ? null : jo["OpenId"].ToString(); 或者string.IsNullOrEmpty(context.Request.Form[name]) ? null : context.Request.Form[name];
asq985 2019-09-25
  • 打赏
  • 举报
回复
用ultraedit打开试试

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧