首页 > 代码库 > WebAPI的Get和Post访问
WebAPI的Get和Post访问
/// <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 = "http://www.mamicode.com/{/"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访问
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。