/// <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. gif显示

    public void gifplay(string path,ref Panel panel) { try{ Bitmap animatedGif = new Bitmap(path ); Grap ...

  2. OC基础--构造方法 id类型

    new方法实现原理: new做了三件事情 1.开辟存储空间  + alloc 方法 2.初始化所有的属性(成员变量) - init 方法 3.返回对象的地址 [Person new]; == [[Pe ...

  3. SQL查询关于相对路径、矢代、绝对路径、递归、计算列的速度对比跟优化-SOD群记录

    1秒查原本递归的查询. 适用于:上下级.多层查询 -- Get childs by parent id WITH Tree AS ( SELECT Id,ParentId FROM dbo.Node ...

  4. mySql 基本语法学习笔记

     create database if not exists yang;    drop database if exists yang;     show databases;   show dat ...

  5. java多线程系列之 synchronized

    一.synchronized基本原理 java的内置锁:每个java对象都可以用做一个实现同步的锁,这些锁成为内置锁.线程进入同步代码块或方法的时候会自动获得该锁,在退出同步代码块或方法时会释放该锁. ...

  6. CSS之照片集效果

    代码: <!DOCTYPE html><html><head> <title>照片影集</title> <meta charset=& ...

  7. hosts持续更新

    Google hosts网址: https://laod.cn/hosts/2016-google-hosts.html

  8. 源码编译安装 MySQL 5.5.x 实践

    1.安装cmakeMySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具.因此,我们首先要在系统中源码编译安装cmake工具. # wget ht ...

  9. SharePoint 2013 中的 PowerPoint Automation Services

    简介                许多大型和小型企业都将其 Microsoft SharePoint Server 库用作 Microsoft PowerPoint 演示文稿的存储库.所有这些企业在 ...

  10. redhat6.5修改yum为163源

    把需要的安装包和配置文件打包,将命令整合到sh文件中,下载后解压运行yun_config.sh 即可 下载链接