標籤:row encode targe .post gas .com oid rem reg
本文來自:https://www.jb51.net/article/62932.htm
/// <summary>/// 分析 url 字串中的參數資訊/// </summary>/// <param name="url">輸入的 URL</param>/// <param name="baseUrl">輸出 URL 的基礎部分</param>/// <param name="nvc">輸出分析後得到的 (參數名,參數值) 的集合</param>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;}
/// <summary>/// 分析 url 字串中的參數資訊/// </summary>/// <param name="url">輸入的 URL</param>/// <param name="baseUrl">輸出 URL 的基礎部分</param>/// <param name="nvc">輸出分析後得到的 (參數名,參數值) 的集合</param>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<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();paramList.Add(new KeyValuePair<string, string>("data","test"));using (HttpClient hc = new HttpClient()){ HttpResponseMessage response = hc.PostAsync(new Uri(url), new FormUrlEncodedContent(paramList)).Result; var result = JsonConvert.DeserializeObject<ResultInfo>(response.Content.ReadAsStringAsync().Result);}
[轉][C#]拆分參數對