标签:coding i++ substring cas encoding index ali query http
/// <summary>
/// 解析查询字符串
/// </summary>
private NameValueCollection GetQueryString(string queryString, Encoding encoding, bool isEncoded)
{
var result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
if (!string.IsNullOrEmpty(queryString))
{
int count = queryString.Length;
for (int i = 0; i < count; i++)
{
int startIndex = i;
int index = -1;
while (i < count)
{
char item = queryString[i];
if (item == ‘=‘)
{
if (index < 0)
{
index = i;
}
}
else if (item == ‘&‘)
{
break;
}
i++;
}
string key = null;
string value = null;
if (index >= 0)
{
key = queryString.Substring(startIndex, index - startIndex);
value = queryString.Substring(index + 1, (i - index) - 1);
}
else
{
key = queryString.Substring(startIndex, i - startIndex);
}
if (isEncoded)
{
result[HttpUtility.UrlDecode(key, encoding)] = HttpUtility.UrlDecode(value, encoding);
}
else
{
result[key] = value;
}
if ((i == (count - 1)) && (queryString[i] == ‘&‘))
{
result[key] = string.Empty;
}
}
}
return result;
}
/// <summary>
/// 解析Key=Value,字符串
/// </summary>
private NameValueCollection GetContentString(string queryString)
{
var result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
if (string.IsNullOrEmpty(queryString))
return result;
int count = queryString.Length;
for (int i = 0; i < count; i++)
{
int startIndex = i;
int index = -1;
while (i < count)
{
char item = queryString[i];
if (item == ‘=‘)
{
if (index < 0)
{
index = i;
}
}
else if (item == ‘,‘)
{
break;
}
i++;
}
string key = null;
string value = null;
if (index >= 0)
{
key = queryString.Substring(startIndex, index - startIndex).Trim();
value = queryString.Substring(index + 1, (i - index) - 1);
}
else
{
key = queryString.Substring(startIndex, i - startIndex).Trim();
}
result[key] = value;
if ((i == (count - 1)) && (queryString[i] == ‘,‘))
{
result[key] = string.Empty;
}
}
return result;
}
标签:coding i++ substring cas encoding index ali query http
原文地址:https://www.cnblogs.com/rsls/p/13038704.html