键值对解析
///
/// 键值对解析Helper,修改matchKey作为键值之间的符号,matchValue为键值对之间的符号
///
public static class KeyValueHelper
{
static Dictionary
///
/// 解析输入Bytes中的键值对
///
/// 输入字节数组
///
public static Dictionary
{
conents.Clear();
conents = GetConentByString(Encoding.Default.GetString(data));
return conents;
}
///
/// 解析输入字符串中的键值对
///
/// 输入字符串
///
public static Dictionary
{
conents.Clear();
if (data.Substring(data.Length - 1) != matchValue)
{
data = data + matchValue;
}
try
{
int pos = 0;
int startIndex = 0;
while (true)
{
//Get Key
pos = data.IndexOf(matchKey, startIndex);
string key = data.Substring(startIndex, pos - startIndex);
startIndex = pos + 1;
//Get Value
pos = data.IndexOf(matchValue, startIndex);
string value = data.Substring(startIndex, pos - startIndex);
startIndex = pos + 1;
conents.Add(key, value);
if (startIndex >= data.Length)
{
break;
}
}
}
catch (Exception ex)
{
throw new Exception("Error Info: " + ex.ToString());
}
return conents;
}
}
//uesrId=1&datetime=2019-10-14 11:43:56&sign=123456sdfsdgdsgsdgfsd
Request.Content.ReadAsStreamAsync().Result.Seek(0, System.IO.SeekOrigin.Begin);
string content = Request.Content.ReadAsStringAsync().Result;
KeyValueHelper.matchKey = "=";
KeyValueHelper.matchValue = "&";
Dictionary
string str = Newtonsoft.Json.JsonConvert.SerializeObject(conents);
JObject obj = JObject.Parse(str);
太麻烦了,来回转好几次,如果没有必要就不要用这种了,我只是想研究一下API POST接收键值对参数,不用定义类的的一种方式,欢迎来喷!!