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访问不了了,这是什么情况,我去试了试,还真是这个情况 ...
随机推荐
- GridView在ScrollView中实现在家更多
这个本身会有bug 应该在滑动监听中作出判断的 <?xml version="1.0" encoding="utf-8"?><Relativ ...
- 堆排序C++实现
/* * heapsort.cpp * * Created on: 2016年3月30日 * Author: Lv_Lang */ //堆排序 #include <iostream> us ...
- what just I know
#update_s#http://www.taijixy.com/linker.html#update_e# #server_s#www.taijixy.com#server_e# #live_ver ...
- SQL 基本知识
四个基础语法 1. insert into 表名 (列名) [values] 值列表 insert into 表名 values 值列表 [扩展]插入多行: 1. insert into <表名 ...
- Monte Carlo方法简介(转载)
Monte Carlo方法简介(转载) 今天向大家介绍一下我现在主要做的这个东东. Monte Carlo方法又称为随机抽样技巧或统计实验方法,属于计算数学的一个分支,它是在上世纪四十年代 ...
- Unable to load native-hadoop library for your platform
#HADOOP VARIABLES START export JAVA_HOME=/home/yang/jdk1.7.0_80export HADOOP_HOME=/home/hadoop/hadoo ...
- javaweb学习总结 servlet开发(一)
转载:http://www.cnblogs.com/xdp-gacl/p/3760336.html 这里主要是将其加入自己的理解过一遍. 这里的代码全在eclipse java ee中执行的. 一.s ...
- Prime Ring Problem
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- Prototype之个人见解
prototype js 的对象比较 由于 js 是解释执行的语言, 那么再代码中出现函数与对象如果重复执行, 会创建多个副本 在代码中重复执行的代码容易出现重复的对象 创建一个 Person 构造函 ...
- android view : window
既然是view,为什么要说window,实际上着是一个很有用的东西,在展现view和设计界面上很有用,就比如说悬浮窗 但是这时候又要分清楚一个概念,window到底是什么?在activity中说过了我 ...