asp.net 网站获取请求的body写入到文件中

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web.UI;
using NLog;

namespace URL
{
    /// <summary>
    /// index
    /// </summary>
    public partial class index : Page
    {
        public static string to_json(object obj)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod.ToLower() == "get")
            {
                Response.Redirect("index.html");
                return;
            }

            if (Request.HttpMethod.ToLower() == "post")
            {
                Request.InputStream.Seek(0, SeekOrigin.Begin);

                using (StreamReader reader = new StreamReader(Request.InputStream, Encoding.UTF8))
                {
                    string requestBody = reader.ReadToEnd();

                    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");

                    if (!Directory.Exists(path)) Directory.CreateDirectory(path);

                    path = Path.Combine(path, string.Format("{0}_data.txt", DateTime.Now.ToString("yyyyMMddHHmmssfff")));

                    File.WriteAllText(path, requestBody);

                    LogManager.GetCurrentClassLogger().Info("\n");
                    LogManager.GetCurrentClassLogger().Info(DateTime.Now.ToString);
                    LogManager.GetCurrentClassLogger().Info("--------- post data ----------");
                    LogManager.GetCurrentClassLogger().Info(requestBody);
                    LogManager.GetCurrentClassLogger().Info("--------- end ----------");

                    // Dictionary<string, string> dictionary2 = new Dictionary<string, string>()
                    // {
                    //     {
                    //         "access_token",
                    //         "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiQU1aVjQ1Nzg5Iiwic2NvcGUiOiJzZWxsaW5nX3BhcnRuZXIiLCJleHAiOjE3NDg2MjQwMDB9.1a2b3c4d5e6f7g8h9i0j"
                    //     },
                    //     {
                    //         "refresh_token",
                    //         "eyJhbGciOiJSUzI1NiIsImtpZCI6IjE2Nzk5NjI2YmZlMDQzZTBiYzI5NTEwMTE4ODA3YWExIn0.eyJqdGkiOiJfUV8ycjY4UG82ekxPalR1SEh5bHlRIiwiaWF0IjoxNzQ4NTkwMTg5LCJleHAiOjE3NDg2NzY1ODksIm5iZiI6MTc0ODU5MDEyOSwic3ViIjoieXVueWEiLCJpc3MiOiJyYW5rIiwiYXVkIjoic2VsbGVyU3BhY2UiLCJpZCI6MTQzMzQ3NCwicGkiOm51bGwsIm5uIjoi6Z2S5bKb5Lqk6YCa56eR5oqAIiwic3lzIjoiU1NfQ04iLCJlZCI6Ik4iLCJwaG4iOiIxNTExMjU1MTc3MiIsImVtIjoiNTAyMDQ4MjI3QHFxLmNvbSIsIm1sIjoiRyJ9.W8i3yYxZ1nnvR1DO0vXN_MqaRMKz5cXYRQVAjEdWcloUda0IY8Jx8irwu2RQzr0LQz6Fc8rb4WUENWvW8jMgs4CoKUNtMams_2NaM9G5ATqCNFhzFWy8bqZw_kCIgvKp4hgDpQJVmlLScRr7omRLJyIXcfJyueuOMe3PPq3D_ThUGHgoMIwdoxsz3OIhhs950aBD74gpTOPhTVSwONlbq5bZBxvTiXBt5dTJeNrVrC2l-9lNbmESAF7tAeq0PlPutN28IPaaF0lFgHyG2DaRJ1ECV9qlOvAxyTyO5YHMUV_6eEDwG5cgIRa2Gfy8SaqYWVrTHk1cmGOq4Va29fjm7w"
                    //     }
                    // };

                    Dictionary<string, object> dictionary = new Dictionary<string, object>()
                    {
                        {"code", 200},
                        {"message", "success"},
                        {
                            "data", new DataTable()
                        },
                        {"time", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff")}
                    };

                    Response.ContentType = "application/json";
                    Response.Write(to_json(dictionary));
                    Response.End();
                }
            }
        }
    }
}

  

done

 


posted @ 2025-06-03 15:56  liskov_design  阅读(19)  评论(0)    收藏  举报