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

WebAPI的Get和Post访问

时间:2016-11-18 12:08:56      阅读:496      评论:0      收藏:0      [点我收藏+]

标签:return   string   按钮   var   bapi   empty   返回   cat   format   

        /// <summary>
        /// MVC 调用webapi接口返回框架快捷按钮和一级菜单(Get)
        /// </summary>
        /// <param name="userId">登录名</param>
        /// <param name="sysShortName">系统简称 如:crm scm ssn</param>
        /// <param name="pwd">密码</param>
        /// <param name="factoryId">工厂编号</param>
        /// <returns></returns>
        public ShortcutResult ReturnShortcutBtn(string userId, string sysShortName, string pwd, string factoryId)
        {
            ShortcutResult shrotcut_result = new ShortcutResult();
            try
            {
                using (var httpClient = new HttpClient())
                {
                    string WebAPIUrl = string.Format(ReturnAppSettingValue("OAShortcut") + "?sysShortName={0}&uid={1}&pwd={2}&factoryId={3}", sysShortName, userId, pwd, factoryId);

                    var task = httpClient.GetAsync(WebAPIUrl);
                    var result = task.Result.Content.ReadAsStringAsync().Result;
                    if (task.Result.IsSuccessStatusCode == false)
                        throw new Exception("接口访问失败:" + task.Result.StatusCode);

                    MenuResult list = JsonConvert.DeserializeObject<MenuResult>(result);

                    if (list != null && list.Success)
                    {
                        var firstMenu = JsonConvert.DeserializeObject<List<FirstLvlMenu>>(list.Menu).OrderBy(t => t.Menu_order).ToList();
                        var btns = JsonConvert.DeserializeObject<List<ShortCutBtn>>(list.Button).OrderBy(t => t.Btn_Order).ToList();

                        shrotcut_result = new ShortcutResult(firstMenu, btns);
                    }
                    else
                    {
                        throw new Exception("WebAPI无值返回或访问失败");
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message, LogEnum.Error);
                throw ex;
            }

            return shrotcut_result;
        }

  

        /// <summary>
        /// MVC 调用webapi接口返回退货单详情页操作按钮(Post)
        /// </summary>
        /// <returns></returns>
        public MenuCommand ReturnCommandBtn(string userId, string menuId, string pwd, string factoryId)
        {
            MenuCommand menuCmd_result = new MenuCommand();
            try
            {
                using (var httpClient = new HttpClient())
                {
                    string WebAPIUrl = ReturnAppSettingValue("ReturnGoodsCommandBtn");
                    HttpContent postContent = new FormUrlEncodedContent(new Dictionary<string, string>()
                    {
                       {"menuId",menuId},
                       {"uid",userId},
                       {"pwd",pwd},
                       {"factoryId",factoryId}
                    });
                    var task = httpClient.PostAsync(WebAPIUrl, postContent);
                    var result = task.Result.Content.ReadAsStringAsync().Result;
                    if (task.Result.IsSuccessStatusCode == false)
                        throw new Exception("接口访问失败:" + task.Result.StatusCode);

                    if (!string.IsNullOrEmpty(result))
                    {
                        menuCmd_result = JsonConvert.DeserializeObject<MenuCommand>(result);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message, LogEnum.Error);
                throw ex;
            }

            return menuCmd_result;
        }

  

        //传递Json格式串

     /// <summary>
        /// 返回Token
        /// </summary>
        /// <returns></returns>
        public APIToken ReturnToken(string userId, string pwd)
        {
            APIToken token = new APIToken();
            try
            {
                string WebAPIUrl = ReturnAppSettingValue("ReturnGoodsToken"); 
                string postData = "{\"UserId\":\"" + userId + "\",\"PassWord\": \"" + pwd + "\"}";

                HttpContent httpContent = new StringContent(postData);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                HttpClient httpClient = new HttpClient();

                HttpResponseMessage response = httpClient.PostAsync(WebAPIUrl, httpContent).Result;

                if (response.IsSuccessStatusCode)
                {
                    string responseJson = response.Content.ReadAsStringAsync().Result;
                    token = JsonConvert.DeserializeObject<APIToken>(responseJson);
                }
                else
                {

                } 
                
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message, LogEnum.Error);
                throw ex;
            }
            return token;
        }

  

WebAPI的Get和Post访问

标签:return   string   按钮   var   bapi   empty   返回   cat   format   

原文地址:http://www.cnblogs.com/seanchang/p/6077003.html

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