[转][C#]拆分参数对
标签:row encode targe .post gas .com oid rem reg
本文来自:https://www.jb51.net/article/62932.htm
///
/// 分析 url 字符串中的参数信息
///
/// 输入的 URL
/// 输出 URL 的基础部分
/// 输出分析后得到的 (参数名,参数值) 的集合
public static NameValueCollection ParseUrl(string url)
{
NameValueCollection nvc = new NameValueCollection();
if (string.IsNullOrEmpty(url))
return nvc;
// 开始分析参数对
Regex re = new Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", RegexOptions.Compiled);
MatchCollection mc = re.Matches(url);
foreach (Match m in mc)
{
nvc.Add(m.Result("$2").ToLower(), m.Result("$3"));
}
return nvc;
}
///
/// 分析 url 字符串中的参数信息
///
/// 输入的 URL
/// 输出 URL 的基础部分
/// 输出分析后得到的 (参数名,参数值) 的集合
public static void ParseUrl(string url, out string baseUrl, out NameValueCollection nvc)
{
if (url == null)
throw new ArgumentNullException("url");
nvc = new NameValueCollection();
baseUrl = "";
if (url == "")
return;
int questionMarkIndex = url.IndexOf(‘?‘);
if (questionMarkIndex == -1)
{
baseUrl = url;
return;
}
baseUrl = url.Substring(0, questionMarkIndex);
if (questionMarkIndex == url.Length - 1)
return;
string ps = url.Substring(questionMarkIndex + 1);
// 开始分析参数对
Regex re = new Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?",RegexOptions.Compiled);
MatchCollection mc = re.Matches(ps);
foreach (Match m in mc)
{
nvc.Add(m.Result("$2").ToLower(), m.Result("$3"));
}
}
List> paramList = new List>();
paramList.Add(new KeyValuePairstring, string>("data","test"));
using (HttpClient hc = new HttpClient())
{
HttpResponseMessage response = hc.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList)).Result;
var result = JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);
}
[转][C#]拆分参数对
标签:row encode targe .post gas .com oid rem reg
原文地址:https://www.cnblogs.com/z5337/p/9221024.html
评论