• webapi get请求 FromUri list参数传递


    今天做项目调用第三方的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("
    
    
    ****************************************************************
    
    
    ");
     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();
  • 相关阅读:
    POJ 3007 Organize Your Train part II (字典树 静态)
    POJ 3096 Surprising Strings
    移动APP安全测试
    monkey亲测
    PHP笔记
    my sql 只展示 前10条数据的写法
    面试问的东西
    定时构建常用 设置
    jenkins 定时构建 位置
    jenkins 使用的python 不是指定的python 的解决方法
  • 原文地址:https://www.cnblogs.com/xcjit/p/4346758.html
Copyright © 2020-2023  润新知