今天做项目调用第三方的webapi时,发现get请求竟然用的是FromUri list参数,汗...
这里简单记录下请求传参的方式(但不推荐大家使用get FromUri list参数 来定义webapi的功能):
1 HttpClient hClient = new HttpClient(new MyHttpClienHanlder()); 2 3 string url = "http://api.test.com/api/pk/index?pkId=2322"; 4 //var ret = hClient.GetAsync(url); 5 //var ss = ret.Result.Content.ReadAsStringAsync().Result; 6 //Console.WriteLine(ss); 7 //Console.WriteLine("/r/n/r/n/r/n****************************************************************/r/n/r/n/r/n"); 8 9 List<long> pIds = new List<long>();10 pIds.Add(2322);11 pIds.Add(1824);12 //List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();13 //paramList.Add(new KeyValuePair<string, string>("pkIds", Newtonsoft.Json.JsonConvert.SerializeObject(pIds)));14 15 string parasFormat = "pkIds[{0}]={1}&";16 StringBuilder paras = new StringBuilder();17 for (var i = 0; i < pIds.Count; i++)18 {19 paras.AppendFormat(parasFormat, i, pIds[i]);20 }21 22 url = "http://api.test.com/api/pk/list?" + paras.Remove(paras.ToString().Length - 1, 1).ToString();23 24 //var ret = hClient.PostAsync(url, new FormUrlEncodedContent(paramList));25 var ret = hClient.GetAsync(url);26 var ss = ret.Result.Content.ReadAsStringAsync().Result;27 Console.WriteLine(ss);28 29 hClient.Dispose();
新闻热点
疑难解答