init
This commit is contained in:
20
DouyinApi.Serilog.Es/HttpInfo/HttpContextProvider.cs
Normal file
20
DouyinApi.Serilog.Es/HttpInfo/HttpContextProvider.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace DouyinApi.Serilog.Es.HttpInfo
|
||||
{
|
||||
public static class HttpContextProvider
|
||||
{
|
||||
private static IHttpContextAccessor _accessor;
|
||||
|
||||
public static HttpContext GetCurrent()
|
||||
{
|
||||
var context = _accessor?.HttpContext;
|
||||
return context;
|
||||
}
|
||||
public static void ConfigureAccessor(IHttpContextAccessor accessor)
|
||||
{
|
||||
_accessor = accessor;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
82
DouyinApi.Serilog.Es/HttpInfo/ParamsHelper.cs
Normal file
82
DouyinApi.Serilog.Es/HttpInfo/ParamsHelper.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
|
||||
namespace DouyinApi.Serilog.Es.HttpInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取参数帮助类
|
||||
/// </summary>
|
||||
public class ParamsHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取参数值
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetParams(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
NameValueCollection form = HttpUtility.ParseQueryString(context.Request.QueryString.ToString());
|
||||
HttpRequest request = context.Request;
|
||||
|
||||
string data = string.Empty;
|
||||
switch (request.Method)
|
||||
{
|
||||
case "POST":
|
||||
|
||||
request.Body.Position = 0;
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
request.Body.CopyTo(ms);
|
||||
var b = ms.ToArray();
|
||||
data = Encoding.UTF8.GetString(b); //把body赋值给bodyStr
|
||||
|
||||
}
|
||||
break;
|
||||
case "GET":
|
||||
//第一步:取出所有get参数
|
||||
IDictionary<string, string> parameters = new Dictionary<string, string>();
|
||||
for (int f = 0; f < form.Count; f++)
|
||||
{
|
||||
string key = form.Keys[f];
|
||||
parameters.Add(key, form[key]);
|
||||
}
|
||||
|
||||
// 第二步:把字典按Key的字母顺序排序
|
||||
IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);
|
||||
IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();
|
||||
|
||||
// 第三步:把所有参数名和参数值串在一起
|
||||
StringBuilder query = new StringBuilder();
|
||||
while (dem.MoveNext())
|
||||
{
|
||||
string key = dem.Current.Key;
|
||||
string value = dem.Current.Value;
|
||||
if (!string.IsNullOrEmpty(key))
|
||||
{
|
||||
query.Append(key).Append("=").Append(value).Append("&");
|
||||
}
|
||||
}
|
||||
data = query.ToString().TrimEnd('&');
|
||||
break;
|
||||
default:
|
||||
data = string.Empty;
|
||||
|
||||
break;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
catch(Exception)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user