public class TinyFoxCommon
{
/// <summary>
/// 获取 post json 数据
/// </summary>
/// <param name="env"></param>
/// <returns></returns>
public static String GetRequestBody(IDictionary<string, object> env)
{
string result = "";
// 获取请求方法
var reqMethod = env["owin.RequestMethod"] as string;
if (reqMethod != "POST" && reqMethod != "PUT") return null;
// 从参数字典中获取 Request HTTP头的字典对象
var reqHeaders = env["owin.RequestHeaders"] as IDictionary<string, string[]>;
// 从字典中获取 Input“流”对象
var reqStream = env["owin.RequestBody"] as Stream;
using (var sr = new StreamReader(reqStream))
{
result = sr.ReadToEnd();//或者sr.ReadToEnd()
}
return result;
}
/// <summary>
/// 获取post from 数据
/// </summary>
/// <param name="env"></param>
/// <returns></returns>
public static Dictionary<string, string> GetRequestFrom(IDictionary<string, object> env)
{
Dictionary<string, string> result = new Dictionary<string, string>();
string requestBody = GetRequestBody(env);
if (!string.IsNullOrWhiteSpace(requestBody))
{
string u = System.Guid.NewGuid().ToString().Replace("-", "");
requestBody = string.Format("?{0}=1&{1}", u, requestBody);
result = GetFrom(requestBody);
}
return result;
}
/// <summary>
/// 获取 Get 数据
/// </summary>
/// <param name="env"></param>
/// <returns></returns>
public static Dictionary<string, string> GetRequestQueryString(IDictionary<string, object> env)
{
Dictionary<string, string> result = new Dictionary<string, string>();
var reqMethod = env["owin.RequestMethod"] as string;
if (reqMethod == "GET")
{
var reqQueryString = env["owin.RequestQueryString"].ToString();
if (!string.IsNullOrWhiteSpace(reqQueryString))
{
string u = System.Guid.NewGuid().ToString().Replace("-", "");
reqQueryString = string.Format("?{0}=1&{1}", u, reqQueryString);
result = GetFrom(reqQueryString);
}
}
//"owin.RequestQueryString"
return result;
}
#region 获取URL 参数
/// <summary>
/// 获取URL 参数
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static Dictionary<string, string> GetFrom(string url)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
url = url.Substring(url.IndexOf('?'), url.Length - url.IndexOf('?')).Replace("?", "");
bool flag = url.IndexOf('&') > 0;
if (flag)
{
string[] array = url.Split('&');
for (int i = 0; i < array.Length; i++)
{
string key = array[i].Split('=')[0];
string value = array[i].Split('=')[1];
dictionary.Add(key, value);
}
}
else
{
string key = url.Split('=')[0];
string value = url.Split('=')[1];
dictionary.Add(key, value);
}
return dictionary;
}
#endregion
}