public class HttpClientBase
{ /// <summary>
/// HttpClient实现Post请求
/// </summary> protected async Task<object> dooPost(string url, Dictionary<string, string> dic)
{ //设置HttpClientHandler的AutomaticDecompression
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
//创建HttpClient(注意传入HttpClientHandler)
using (var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(dic);
//await异步等待回应
var response = await http.PostAsync(url, content);
//确保HTTP成功状态值
response.EnsureSuccessStatusCode(); //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
string Result = await response.Content.ReadAsStringAsync(); var Item = JsonConvert.DeserializeObject(Result); return Item;
}
} /// <summary>
/// HttpClient实现Get请求
/// </summary>
protected async Task<object> dooGet(string url)
{ //创建HttpClient(注意传入HttpClientHandler)
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
string Result = "";
using (var http = new HttpClient(handler))
{
//await异步等待回应
var response = await http.GetAsync(url);
//确保HTTP成功状态值
response.EnsureSuccessStatusCode(); //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
Result = await response.Content.ReadAsStringAsync(); var Items= JsonConvert.DeserializeObject(Result); return Items;
}
} /// <summary>
/// HttpClient实现Put请求
/// </summary>
protected async Task<object> dooPut(string url, Dictionary<string, string> dic)
{ //设置HttpClientHandler的AutomaticDecompression
var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
string Result="";
//创建HttpClient(注意传入HttpClientHandler)
using (var http = new HttpClient(handler))
{
//使用FormUrlEncodedContent做HttpContent
var content = new FormUrlEncodedContent(dic);
//await异步等待回应
var response = await http.PutAsync(url, content);
//确保HTTP成功状态值
response.EnsureSuccessStatusCode();
//await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
JavaScriptSerializer Serializer = new JavaScriptSerializer();
var items = Serializer.DeserializeObject(Result);
return items;
} } /// <summary>
/// HttpClient实现Get请求
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
protected object doGet(string url)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync(url).Result; // Blocking call(阻塞调用)! var result = ""; if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result; JavaScriptSerializer Serializer = new JavaScriptSerializer();
var items = Serializer.DeserializeObject(result);
return items;
}
return result;
} /// <summary>
/// HttpClient实现Post请求
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
protected object doPost(string url, Dictionary<string, string> dic,ref string StatusCode)
{ HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url); JavaScriptSerializer jss = new JavaScriptSerializer(); var content = new FormUrlEncodedContent(dic);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PostAsync(url, content).Result;
var result = "";
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result; JavaScriptSerializer Serializer = new JavaScriptSerializer();
var items = Serializer.DeserializeObject(result);
//Dictionary<string, object> jarry = (Dictionary<string, object>)items;
//obj ss = jarry["IsSuccess"];
//string sate = jarry[0]["IsSuccess"].ToString();
return items;
}else
{
StatusCode = response.StatusCode.ToString(); ;
}
return result;
} /// <summary>
/// 转换URL
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string UrlEncode(string str)
{
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);
for (int i = 0; i < byStr.Length; i++)
{
sb.Append(@"%" + Convert.ToString(byStr[i], 16));
}
return (sb.ToString());
}
}

WebApi帮助类的更多相关文章

  1. C# HttpClient请求Webapi帮助类

    引用 Newtonsoft.Json // Post请求 public string PostResponse(string url,string postData,out string status ...

  2. HTTPClick调用WebApi帮助类

    1.创建ApiHelper类 2.复制以下代码到类中 using System; using System.Collections.Generic; using System.Linq; using ...

  3. 封装WebAPI客户端,附赠Nuget打包上传VS拓展工具

    一.前言 上篇< WebAPI使用多个xml文件生成帮助文档 >有提到为什么会出现基于多个xml文件生成帮助文档的解决方案,因为定义的模型可能的用处有: 1:单元测试 2:其他项目引用(可 ...

  4. WebApi个人理解概要

    WebApi概要 Global文件的作用: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class MvcApplication : System.We ...

  5. WebAPI客户端

    封装WebAPI客户端,附赠Nuget打包上传VS拓展工具 一.前言 上篇< WebAPI使用多个xml文件生成帮助文档 >有提到为什么会出现基于多个xml文件生成帮助文档的解决方案,因为 ...

  6. WebApi实现原理解析笔记

    这是我看过WebApi实现代码后的一些总结,一方面加深自己的记忆,另外也希望能够帮助大家更深入的了解WebApi. 注:暂时没有好好的整理,可能有些晦涩难懂. Webapi 控制器类必须实现IHttp ...

  7. webapi框架搭建-依赖注入之autofac

    前言 c#的依赖注入框架有unity.autofac,两个博主都用过,感觉unity比较简单而autofac的功能相对更丰富(自然也更复杂一点),本篇将基于前几篇已经创建好的webapi项目,引入au ...

  8. webapi框架搭建-创建项目(三)-webapi owin

    上一篇:创建项目(二) 在上一篇里,我们已经创建好了webapi应用,并已经部署到iis里,本篇讲如何用owin自宿主或是iis宿主来部署webapi应用. owin介绍 传统的asp.net网站只能 ...

  9. mvc接口、webapi、webservice 对比

    最近做了很多项目,也同时使用了mvc的接口,webapi的接口,以及webservice 的接口.先两两对比如下: mvc和webapi区别: 1.MVC是建站的一种框架,倾向于返回用户的页面请求:a ...

随机推荐

  1. emoji & click copy

    emoji & click copy document.execCommand("copy"); https://clipboardjs.com/ https://www. ...

  2. js数组去重五种方法

    今天来聊一聊JS数组去重的一些方法,包括一些网上看到的和自己总结的,总共5种方法(ES5). 第一种:遍历数组法 这种方法最简单最直观,也最容易理解,代码如下: var arr = [2, 8, 5, ...

  3. 【设计模式】—— 状态模式State

    前言:[模式总览]——————————by xingoo 模式意图 允许一个对象在内部改变它的状态,并根据不同的状态有不同的操作行为. 例如,水在固体.液体.气体是三种状态,但是展现在我们面前的确实不 ...

  4. 【设计模式】—— 适配器模式Adapter

    前言:[模式总览]——————————by xingoo 模式意图 如果已经有了一种类,而需要调用的接口却并不能通过这个类实现.因此,把这个现有的类,经过适配,转换成支持接口的类. 换句话说,就是把一 ...

  5. 【BZOJ1056】[HAOI2008]排名系统(Splay)

    [BZOJ1056][HAOI2008]排名系统(Splay) 题面 BZOJ 洛谷 题解 \(Splay\)随便维护一下就好了,至于名字什么的,我懒得手写哈希表了,直接哈希之后拿\(map\)压. ...

  6. 【BZOJ4500】矩阵(差分约束)

    [BZOJ4500]矩阵(差分约束) 题面 BZOJ 然而权限题 题解 显然拆分行和列.不妨设这一行/列总共加减的值是\(p\),那么每一个限制就是两个数的和为一个特定的数.这样子不好做,反正是一个二 ...

  7. POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)

    POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...

  8. 【洛谷P1126】机器人搬重物

    题目大意:给定一个 N 行,M 列的地图,一个直径为 1.6 的圆形机器人需要从起点走到终点,每秒钟可以实现:向左转,向右转,向前 1-3 步.求从起点到终点最少要多长时间. 题解:相比于普通的走迷宫 ...

  9. word绘图画布

    这样图形组合不会随着位置的变动而出现相对变化

  10. CronExpression

    CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表.CronTr ...