post参数解析
2012-08-28 11:21
134 查看
#region post参数解析
/// <summary>
/// post参数解析
/// </summary>
/// <param name="s"></param>
/// <param name="isUrlDecode"></param>
/// <param name="encoding"></param>
/// <returns></returns>
private static NameValueCollection FillFromString(string s, bool isUrlDecode, Encoding encoding)
{
NameValueCollection parametersCollection = new NameValueCollection();
// 参数字符串长度
int sLen = (s != null) ? s.Length : 0;
// 遍历每个字符
for (int i = 0; i < sLen; i++)
{
// 参数名开始位置
int startIndex = i;
// 参数名结束位置
int endIndex = -1;
// 字符索引前进,直到遇到等号,更新结束的索引位置
// 如果遇到&符号,则参数结束,退出循环
while (i < sLen)
{
char ch = s[i];
if (ch == '=')
{
if (endIndex < 0)
{
endIndex = i;
}
}
else if (ch == '&')
{
break;
}
// 字符索引前进
i++;
}
string parameterName = null;
string parameterValue = null;
// 存在等号,可以获取到参数名和参数值
if (endIndex >= 0)
{
parameterName = s.Substring(startIndex, endIndex - startIndex);
parameterValue = s.Substring(endIndex + 1, (i - endIndex) - 1);
}
else
{
parameterValue = s.Substring(startIndex, i - startIndex);
}
// 需要解码
if (isUrlDecode)
{
parametersCollection.Add(HttpUtility.UrlDecode(parameterName, encoding), HttpUtility.UrlDecode(parameterValue, encoding));
}
else
{
parametersCollection.Add(parameterName, parameterValue);
}
// 最后一个字符是 &,则添加一个参数为null的NameValue对。
if ((i == (sLen - 1)) && (s[i] == '&'))
{
parametersCollection.Add(null, string.Empty);
}
}
return parametersCollection;
}
#endregion
/// <summary>
/// post参数解析
/// </summary>
/// <param name="s"></param>
/// <param name="isUrlDecode"></param>
/// <param name="encoding"></param>
/// <returns></returns>
private static NameValueCollection FillFromString(string s, bool isUrlDecode, Encoding encoding)
{
NameValueCollection parametersCollection = new NameValueCollection();
// 参数字符串长度
int sLen = (s != null) ? s.Length : 0;
// 遍历每个字符
for (int i = 0; i < sLen; i++)
{
// 参数名开始位置
int startIndex = i;
// 参数名结束位置
int endIndex = -1;
// 字符索引前进,直到遇到等号,更新结束的索引位置
// 如果遇到&符号,则参数结束,退出循环
while (i < sLen)
{
char ch = s[i];
if (ch == '=')
{
if (endIndex < 0)
{
endIndex = i;
}
}
else if (ch == '&')
{
break;
}
// 字符索引前进
i++;
}
string parameterName = null;
string parameterValue = null;
// 存在等号,可以获取到参数名和参数值
if (endIndex >= 0)
{
parameterName = s.Substring(startIndex, endIndex - startIndex);
parameterValue = s.Substring(endIndex + 1, (i - endIndex) - 1);
}
else
{
parameterValue = s.Substring(startIndex, i - startIndex);
}
// 需要解码
if (isUrlDecode)
{
parametersCollection.Add(HttpUtility.UrlDecode(parameterName, encoding), HttpUtility.UrlDecode(parameterValue, encoding));
}
else
{
parametersCollection.Add(parameterName, parameterValue);
}
// 最后一个字符是 &,则添加一个参数为null的NameValue对。
if ((i == (sLen - 1)) && (s[i] == '&'))
{
parametersCollection.Add(null, string.Empty);
}
}
return parametersCollection;
}
#endregion
相关文章推荐
- 实习第一天——做一个录音上传服务器的小功能pttp协议post发送文件和参数到服务器,解析返回的json数据
- java post 请求参数传递,响应解析
- express 解析post方式下的json参数
- post请求参数格式的分类解析
- 用HttpListener做web服务器,简单解析post方式过来的参数、上传的文件
- Netty4 HTTP请求参数解析(GET, POST)
- tomcat中解析url中的参数或者post中的请求内容
- php 把$_POST参数解析出来
- 解析GB18030编码格式的POST请求参数
- 用HttpListener做web服务器,简单解析post方式过来的参数、上传的文件
- python http请求post参数,json解析
- WebAPI从Post的Body中解析出JObject格式的Request参数
- Android客户端发送Post/Get请求到Asp.Net服务端一般处理程序Asp.Net进行参数解析
- Netty实现java多线程Post请求解析(Map参数类型)—SKY
- Netty4 HTTP请求参数解析(GET, POST)
- 【WPF】C#用POST请求参数含中文,服务器解析得到乱码问题
- 接口测试入门(3)--使用httpClient进行登录用例操作/set-cookies验证/ List<NameValuePair>设置post参数/json解析
- post网络请求,支持多层字典参数
- Drupal::httpClient中post,get参数参考
- Jsp页面中文参数传递get和post方法分析