Key-value pairs to resolve

/// <Summary>
/// key-value pair parsed Helper, as modified matchKey keys between symbols, matchValue between the symbols on the key
/// </ Summary>
public static class KeyValueHelper
{
static the Dictionary <Object , Object> = new new conents the Dictionary <Object, Object> ();
public static String matchKey {GET; SET;}
public static String matchValue {GET; SET;}

/// <Summary>
/// Bytes parse the input key-value pairs
/// </ Summary>
/// <param name = "Data"> the input byte array </ param>
/// <Returns> Analytical after the key-value dictionary </ Returns>
public static the Dictionary <Object, Object> GetConentByBytes (byte [] Data)
{
conents.Clear ();
conents = GetConentByString (Encoding.Default.GetString (Data));
return conents;
}

/// <Summary>
/// parse the input string key-value pairs
/// </ Summary>
/// <param name = "Data"> input string </ param>
/// <Returns> Analytical after the key-value dictionary </ Returns>
public static the Dictionary <Object, Object> GetConentByString (String Data)
{
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<object, object> conents = KeyValueHelper.GetConentByString(content);
string str = Newtonsoft.Json.JsonConvert.SerializeObject(conents);
JObject obj = JObject.Parse(str);

 

 

Too much trouble, turning several times to, if not necessary do not use this, I just want to look at API POST receives the key parameters, without a way to define the class, a welcome to spray! !

 

Guess you like

Origin www.cnblogs.com/ztf20/p/11926815.html