/// <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访问的更多相关文章

  1. As.net WebAPI CORS, 开启跨源访问,解决错误No 'Access-Control-Allow-Origin' header is present on the requested resource

    默认情况下ajax请求是有同源策略,限制了不同域请求的响应. 例子:http://localhost:23160/HtmlPage.html 请求不同源API http://localhost:228 ...

  2. Asp.Net WebApi 启用CORS跨域访问指定多个域名

    1.后台action指定 EnableCors指定可访问的域名多个,使用逗号隔开 //支持客户端凭据提交,指定多个域名,使用逗号隔开 [EnableCors("http://localhos ...

  3. WebApi(三)-属性路由 自定义访问路径

    启用属性路由: 1.在WebApiConfig中加入如下代码: //属性路由 config.MapHttpAttributeRoutes();

  4. WebApi不支持跨域访问

  5. WebApi PUT与DELETE类型访问报错

    * 方法一 在项目的Web.Config文件加入 <modules> <remove name="WebDAVModule" /> </modules ...

  6. Asp.Net WebApi+Microsoft.AspNet.WebApi.Core 启用CORS跨域访问

    WebApi中启用CORS跨域访问 1.安装 Nugget包Microsoft.AspNet.WebApi.Cors This package contains the components to e ...

  7. html网页访问WebAPI中的方法遇到的问题

      1.移动端访问远程服务时,建议使用WebAPI 2.用不同浏览器访问WebAPI时返回的文本格式是不同的,Chrome Firefox将在浏览器中以XML形式显示此列表,IE浏览器将获得Json格 ...

  8. layui表单提交与ajax访问webapi

    啊啊啊啊 这个东西实在很蛋疼啊 每次访问webapi就很老火 这里就一下  以后忘记的话就来查阅 不多说 直接开始 首先html页面 新建一个基于layui的form表单页面LayuiForm.csh ...

  9. WebAPi之SelfHost自创建证书启动Https疑难解惑及无法正确返回结果

    前言 话说又来需求了,之前对于在SelfHost中需要嵌套页面并操作为非正常需求,这回来正常需求了,客户端现在加了https,老大过来说WebAPi访问不了了,这是什么情况,我去试了试,还真是这个情况 ...

随机推荐

  1. 搬运:Python for Windows——监控Windows某个目录下文件的变化

    https://win32com.goermezer.de/content/view/286/285/ 这个网站真是给力,不多说,代码直接搬运过来,还有我的测试结果,拿走不谢! import os i ...

  2. JS 跑马灯

    利用jquery 来实现图片切换.文字转换移动的工具. MSClass 连接 http://www.popub.net/script/MSClass.html Mark 用

  3. VMware下利用ubuntu13.04建立嵌入式开发环境之四

    二.telnet.SSH服务器安装与配置 1.telnet 1.1 安装服务器:apt-get install xinetd telnetd 1.2 安装openbsd-inetd:apt-get i ...

  4. Java泛型学习笔记 - (二)泛型类

    1. 我们先写一个没有泛型的类Box: public class Box { private Object obj; public Box() {} public Object getObj() { ...

  5. C语言的编译过程、安装gcc编译器以及设置环境变量

    以我对C语言编译过程的了解,我用了一点时间画了一个图,提供给大家参考一下,希望有些能对您的问题提上帮助. 前几天刚初步学习了C语言的编译过程,感触挺深的.在C语言中头文件其实起了一个很大的作用. 1. ...

  6. what is SVD and how to calculate it

    http://web.mit.edu/be.400/www/SVD/Singular_Value_Decomposition.htm SVD是研究地震波运动极性化的一个方法.

  7. Linq 备忘录

    public class CTest { public int i { get; set; } public string j { get; set; } } 一.Range var items=En ...

  8. iOS 上拉下拉刷新简单实现代码

    一般说到上拉刷新下拉刷新,很多人可能想到的是一个第三方开源框架EGORefresh,下面说下,如何自己写代码实现. UITableView本身是一个UIScrollView,所以UITableView ...

  9. GeoHash原理解析

    GeoHash 核心原理解析       引子 一提到索引,大家脑子里马上浮现出B树索引,因为大量的数据库(如MySQL.oracle.PostgreSQL等)都在使用B树.B树索引本质上是对索引字段 ...

  10. 关于解析P D X P 协议的心得

    1一个线程进队Quee 一个线程出队 也应该lock,不然会出错. 2 委托的效率较低 能不用委托的地方,尽量不要用委托. 在一个线程中需要调用控件时采用委托. 3 for循环中异步发送数据不能保证发 ...