You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.8 KiB
63 lines
1.8 KiB
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
|
|
namespace WcfControlMonitorWebLib
|
|
{
|
|
/// <summary>
|
|
/// HTTP 请求的扩展方法
|
|
/// </summary>
|
|
internal static class RequestExtension
|
|
{
|
|
/// <summary>
|
|
/// 获取令牌,即服务端对应的 Session id
|
|
/// </summary>
|
|
internal static string Token(this HttpRequestMessage request)
|
|
{
|
|
if (request.Headers.Contains("token"))
|
|
{
|
|
return request.Headers.GetValues("token").First();
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 以动态类型解析请求数据
|
|
/// </summary>
|
|
internal static dynamic Data(this HttpRequestMessage request)
|
|
{
|
|
return request.Data<dynamic>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解析请求数据,并尝试转换为指定类型
|
|
/// </summary>
|
|
internal static T Data<T>(this HttpRequestMessage request)
|
|
{
|
|
var obj = new JObject();
|
|
foreach (KeyValuePair<string, object> item in request.GetRouteData()?.Values)
|
|
{
|
|
obj.Add(item.Key, new JValue(item.Value));
|
|
}
|
|
foreach (KeyValuePair<string, string> item in request.GetQueryNameValuePairs())
|
|
{
|
|
obj.Add(item.Key, item.Value);
|
|
}
|
|
var task = request.Content.ReadAsAsync<JToken>();
|
|
task.Wait();
|
|
if (task.Result != null)
|
|
{
|
|
foreach (var item in task.Result)
|
|
{
|
|
obj.Add(item);
|
|
}
|
|
}
|
|
return obj.ToObject<T>();
|
|
}
|
|
}
|
|
}
|