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 = "{\"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访问的更多相关文章
- 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 ...
- Asp.Net WebApi 启用CORS跨域访问指定多个域名
1.后台action指定 EnableCors指定可访问的域名多个,使用逗号隔开 //支持客户端凭据提交,指定多个域名,使用逗号隔开 [EnableCors("http://localhos ...
- WebApi(三)-属性路由 自定义访问路径
启用属性路由: 1.在WebApiConfig中加入如下代码: //属性路由 config.MapHttpAttributeRoutes();
- WebApi不支持跨域访问
- WebApi PUT与DELETE类型访问报错
* 方法一 在项目的Web.Config文件加入 <modules> <remove name="WebDAVModule" /> </modules ...
- Asp.Net WebApi+Microsoft.AspNet.WebApi.Core 启用CORS跨域访问
WebApi中启用CORS跨域访问 1.安装 Nugget包Microsoft.AspNet.WebApi.Cors This package contains the components to e ...
- html网页访问WebAPI中的方法遇到的问题
1.移动端访问远程服务时,建议使用WebAPI 2.用不同浏览器访问WebAPI时返回的文本格式是不同的,Chrome Firefox将在浏览器中以XML形式显示此列表,IE浏览器将获得Json格 ...
- layui表单提交与ajax访问webapi
啊啊啊啊 这个东西实在很蛋疼啊 每次访问webapi就很老火 这里就一下 以后忘记的话就来查阅 不多说 直接开始 首先html页面 新建一个基于layui的form表单页面LayuiForm.csh ...
- WebAPi之SelfHost自创建证书启动Https疑难解惑及无法正确返回结果
前言 话说又来需求了,之前对于在SelfHost中需要嵌套页面并操作为非正常需求,这回来正常需求了,客户端现在加了https,老大过来说WebAPi访问不了了,这是什么情况,我去试了试,还真是这个情况 ...
随机推荐
- MicroERP数据初始化SQL脚本
--use MicroERP insert into tbUserGroup(GroupName,Remark) values('管理员组','具备所有权限')insert into tbUser(L ...
- .net版高斯模糊算法
最近挺多人找高斯算法,本人贴上一个高斯模糊算法类,希望可以帮助到大家.算法的效率还是可以接受的. #region 高斯模糊算法 /// <summary> /// 高斯模糊算法 /// & ...
- 第八篇.Bootstrap下拉菜单
给div一个class “dropdown”,将下拉菜单放在这个div里就可以显示bootstrap的下拉菜单. <div class="dropdown"> < ...
- Buffer、Channel示例
a.txt 孔雀向西飞,今朝更好看.孔雀向西飞,今朝更好看.孔雀向西飞,今朝更好看.孔雀向西飞,今朝更好看. 示例一. package com.test; import java.io.FileI ...
- hibernate开发(1)
1 什么是ORM OR-mapping 是 object relational mapping 对象关系映射,是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换.解决了不同数据库s ...
- 3、Android Intent Flag的介绍
介绍:http://blog.csdn.net/luckily01/article/details/7737499
- JQuery中$.ajax()方法参数都有哪些?
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...
- Python学习路程day17
常用算法与设计模式 选择排序 时间复杂度 二.计算方法 1.一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费 ...
- 黑马程序员——【Java高新技术】——案例:银行业务调度系统
---------- android培训.java培训.期待与您交流! ---------- 一.银行业务调度系统需求 Ø 银行内有6个业务窗口,1 - 4号窗口为普通窗口,5号窗口为快速窗口,6号窗 ...
- JS小总结
今天看了一下js的部分总结了一下 函数的定义首先是命名函数function funcName函数还有匿名函数 var funcName = function()普通函数和匿名函数在调用上基本没有什么区 ...