System.Net.Http
System.Net.Http
DotNet菜园
占个位置-
2018-11-10 09:55:00修改
这个HttpClient的学习笔记一直迟迟未记录,只引用了其他博主的博客链接占个位置,但被浏览量(138,另外一篇System.Speech使用浏览130)竟然是我截止2018-11-10时最多的,这让咱情何以堪。
在项目中还是有蛮多需要用到云服务的,比如调用百度翻译API,云报警等功能中都会用到,实现这部分功能并不困难,但是要写的整洁漂亮还是要花点心思的。咱是一个代码整洁追求者,发现Go语言对这方面很有追求,咱也很喜欢这门语言,目前也在学习当中。呀,扯远了,还是说说一些在C#中如何使用HttpClient吧。
HttpClient提供了Get和Post的操作,这两个操作应该是能满足我们99%的应用场景了。都提供了异步执行。会将这部分的使用根据应用场景进行封装,让自己使用的得心应手,手到擒来,岂不美哉。以下是开始封装的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
namespace WindowsFormsApp1
{
public static class StCloud
{
private static string Url => "http://www.baidu.com/";
private static HttpClient httpClient;
static StCloud()
{
HttpClientHandler httpClientHandler = new HttpClientHandler();
httpClientHandler.UseCookies = true;
httpClient = new HttpClient(httpClientHandler);
}
public static string Get(string api)
{
string url = $"{Url}{api}";
return httpClient.GetStringAsync(url).Result;
}
public static string Post(string api, Dictionary<string, string> dict)
{
string url = $"{Url}{api}";
FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent(dict);
return httpClient.PostAsync(url, formUrlEncodedContent).Result.Content.ReadAsStringAsync().Result;
}
public static string Post(string api, string txt)
{
string url = $"{Url}{api}";
StringContent stringContent = new StringContent(txt);
return httpClient.PostAsync(url, stringContent).Result.Content.ReadAsStringAsync().Result;
}
}
}
我习惯于将各个功能写一个函数来条用,例如基本的登录:
public static string Login(Dictionary<string, string> dict)
{
string api = "login";
string result = Post(api, dict);
return result;
}
但我还会进一步处理登录时返回的信息。
public static LoginResult Login(Dictionary<string, string> dict)
{
string api = "login";
string result = Post(api, dict);
LoginResult loginResult = new LoginResult(result);
return loginResult;
}
public class ResultBase
{
public string ErrorCode { get; set; }
public string Msg { get; set; }
}
public class LoginResult : ResultBase
{
public bool Successed { get; set; }
public LoginResult(string content)
{
//对信息进行处理
}
}
System.Net.Http的更多相关文章
- .Net多线程编程—System.Threading.Tasks.Parallel
System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Parallel.For,Parallel.ForEach这三个静态方法. 1 Parallel. ...
- .Net Core MVC 网站开发(Ninesky) 2.2、栏目管理功能-System区域添加
在asp或asp.net中为了方便网站的结构清晰,通常把具有类似功能的页面放到一个文件夹中,用户管理功能都放在Admin文件夹下,用户功能都放在Member文件夹下,在MVC中,通常使用区域(Area ...
- .Net Core上用于代替System.Drawing的类库
目前.Net Core上没有System.Drawing这个类库,想要在.Net Core上处理图片得另辟蹊径. 微软给出了将来取代System.Drawing的方案,偏向于使用一个单独的服务端进行各 ...
- System.Guid ToString五中格式
参考:https://msdn.microsoft.com/en-us/library/97af8hh4.aspx 测试代码: using System; using System.Collectio ...
- System进程(pid=4)占用80端口的解决方案
问题 Mail服务器在安装TFS服务(含SQLServer2016)后启动不了网页服务. 排查问题 使用命令查看端口占用情况 netstat -nao | find ":80" n ...
- -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HO 解决办法
最近在使用maven,项目测试的时候出现了这么一个错.-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2 ...
- .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数
.Net中我们通常使用Random类生成随机数,在一些场景下,我却发现Random生成的随机数并不可靠,在下面的例子中我们通过循环随机生成10个随机数: ; i < ; i++) { Rando ...
- .net正则表达式大全(.net 的 System.Text.RegularExpressions.Regex.Match()方法使用)
正则表达式的本质是使用一系列特殊字符模式,来表示某一类字符串.正则表达式无疑是处理文本最有力的工具,而.NET的System.dll类库提供的System.Text.RegularExpression ...
- System.arraycopy()和Arrays.copyOf()的区别
先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...
- 类型“System.Data.Linq.DataContext”在未被引用的程序集中定义。必须添加对程序集“System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的引用。
解决方法:添加System.Data.Linq.dll引用 http://www.cnblogs.com/m84641693/archive/2010/07/26/1785100.html http: ...
随机推荐
- XAMPP配置8080端口
IIS需要HTTP服务,这个服务占用了80端口. Apache启动不了,为了都可以使用,将Apache端口改为8080.
- Python下OS模块重命名方法renames
在python中有很多强大的模块,其中我们经常要使用的就是OS模块,OS模块提供了超过200个方法来供我们使用,并且这些方法都是和数据处理相关的,这里介绍下重命名这个方法. OS的重命名方法是os.r ...
- 用cascade删除有约束的表或记录
删除有约束的表 Drop table TERMPRO_RULE_ROUTE_TYPE cascade constraints:
- Windows环境下使用.bat安装和卸载服务
一.Windows环境下使用.bat安装和卸载服务 win7环境 例子中“”Valwell.Dms.HttpService.exe“”为服务程序名称 安装服务 %SystemRoot%\Microso ...
- WEB前端必备掌握知识
1.跨域: 跨域问题是由于javascript语言安全限制中的同源策略造成的.
- CImage得到位图的大小
CImage image; image.Load(_T("1.jpg")); //HBITMAP hBitmap=image.Detach(); HGLOBAL m_hMem = ...
- Betsy's Tour 漫游小镇(dfs)
Description 一个正方形的镇区分为 N2 个小方块(1 <= N <= 7).农场位于方格的左上角,集市位于左下角.贝茜穿过小镇,从左上角走到左下角,刚好经过每个方格一次.当 N ...
- Elasticsearch集群如何扩容机器?
前提, Elasticsearch-2.4.3的3节点安装(多种方式图文详解) 比如,你已经成功搭建了3台机器的es集群,如我这里分别是192.168.80.10.192.168.80.11.19 ...
- Spring Cloud Hystrix 1(熔断器简介)
在分布式框架中当某个服务单元发生故障之后通过断路器的故障监控向调用方返回一个错误响应,而不是长期等待这样就不会使得线程因调用故障服务被长时间占用不放,避免了故障在分布式系统中的蔓延 针对上述问题,Sp ...
- eth0 no such device(reload)
转载自:http://blog.chinaunix.net/uid-25554408-id-292638.html 今天我在vmware里安装了虚拟机,安装虚拟机就想安装vmware tools(这个 ...