代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C#
】
WebApiClient
作者:
刺猬拥抱
/ 发布于
2016/5/26
/
1061
using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using Newtonsoft.Json; namespace Common { public class WebApiClientProvider { private readonly string _hostUri; private readonly bool _needToken; public WebApiClientProvider(string hostUri, bool needToken) { _hostUri = hostUri; _needToken = needToken; } public string AccessToken { get; private set; } public Dictionary<string, string> GetTokenDictionary(string userName, string password) { HttpResponseMessage response; var pairs = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("grant_type", "password"), new KeyValuePair<string, string>("username", userName), new KeyValuePair<string, string>("password", password) }; var content = new FormUrlEncodedContent(pairs); using (var client = new HttpClient()) { var tokenEndpoint = new Uri(new Uri(_hostUri), "Token"); response = client.PostAsync(tokenEndpoint, content).Result; } var responseContent = response.Content.ReadAsStringAsync().Result; if (!response.IsSuccessStatusCode) { throw new Exception(string.Format("Error: {0}", responseContent)); } return GetTokenDictionary(responseContent); } private Dictionary<string, string> GetTokenDictionary(string responseContent) { var tokenDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseContent); AccessToken = tokenDictionary["access_token"]; return tokenDictionary; } public HttpResponseMessage Get(string uri) { using (var client = new HttpClient()) { CheckToken(client); return client.GetAsync(new Uri(new Uri(_hostUri), uri)).Result; } } public T GetAsJson<T>(string uri) { using (var client = new HttpClient()) { CheckToken(client); var request = new HttpRequestMessage() { RequestUri = new Uri(new Uri(_hostUri), uri), Method = HttpMethod.Get, }; request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { var content = response.Content.ReadAsStringAsync().Result; var user = JsonConvert.DeserializeObject<T>(content); return user; } return default(T); } } public HttpResponseMessage PostAsJson<T>(string uri, T value) { using (var client = new HttpClient()) { CheckToken(client); HttpContent content = new StringContent(JsonConvert.SerializeObject(value)); return client.PostAsync(new Uri(new Uri(_hostUri), uri).ToString(), content).Result; } } public HttpResponseMessage Post(string uri, HttpContent content) { using (var client = new HttpClient()) { CheckToken(client); return client.PostAsync(new Uri(new Uri(_hostUri), uri).ToString(), content).Result; } } private void CheckToken(HttpClient client) { if (_needToken) client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken); } } }
试试其它关键字
同语言下
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
可能有用的
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
刺猬拥抱
贡献的其它代码
(
14
)
.
如何获取视频的总时长
.
敏感字符串处理工具类
.
字符串字典序排序
.
实现文件夹及文件压缩,并实现下载(二)
.
DataTable转Json Json转DataTable
.
google suggest 下拉菜单(asp.net版本)
.
在SQL2008查找某数据库中的列是否存在某个值
.
WebApiClient
.
json内查询
.
repeater 当前行号
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3