码迷,mamicode.com
首页 > Windows程序 > 详细

C#调取webapi

时间:2017-08-17 17:17:04      阅读:539      评论:0      收藏:0      [点我收藏+]

标签:json   success   com   uid   pass   opened   config   open   ctr   

技术分享
 //定义参数  C#调取webapi
         var content = new FormUrlEncodedContent(new Dictionary<string, string>()
          {
           {"Id",Guid.Empty.ToString()},
           {"data",DateTime.Now.ToString("yyyy-MM-dd") },
           {"new_visitor","123"},
           {"new_visitor_percent","20.00%"},
           {"old_visitor","456"},
           {"old_visitor_percent","30.00%"},
           {"orgid","5e436943-2e51-4d34-baab-762cf58b2ac7"},
           });
           result = RequestAPI(content, "api/SiteService/GetSaveCtrUserCompare");


  public string RequestAPI(FormUrlEncodedContent content, string strmethod)
        {
            //提交当前地址的webapi
            //string apiUrl = ConfigurationManager.AppSettings["SSOPassport"];
            string apiUrl = "http://xxx.xxx.xx.xxx:8077/";
            //向用户中心提交部门
            //后台client方式GET提交
            HttpClient myHttpClient = new HttpClient();
            myHttpClient.BaseAddress = new Uri(apiUrl);
            HttpResponseMessage response = myHttpClient.PostAsync(strmethod, content).Result;
            string result = "";
            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsStringAsync().Result;
            }
            //将json字符串转化为对应的格式
            JObject jsonObj = JObject.Parse(result);
            string dataStr = string.Empty;
            if (jsonObj.Property("Result") != null && jsonObj["Result"] != null && !string.IsNullOrEmpty(jsonObj["Result"].ToString()))
            {
                dataStr = jsonObj["Result"].ToString();
            }
            return dataStr;
        }
View Code

 

C#调取webapi

标签:json   success   com   uid   pass   opened   config   open   ctr   

原文地址:http://www.cnblogs.com/yangwujun/p/7382590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!