多线程之HttpClient
在程序用调用 Http 接口、请求 http 资源、编写 http 爬虫等的时候都需要在程序集中进行 Http 请求。
很多人习惯的 WebClient、HttpWebRequest 在 TPL 下很多用起来不方便的地方,TPL 下推荐使用 HttpClient(using System.Net.Http;),.net core 下已经不支持 WebClient 等
HttpClient 发 出 Get 请 求 获 取 文 本 响 应 : string html = await hc.GetStringAsync("http://www.rupeng.com");
HttpClient 发出Post请求使用
//第一个参数是请求的地址,第二个参数就是用来设置请求内容的
Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content)
HttpContent是一个抽象类:主要的子类有:
- FormUrlEncodedContent(表单格式请求)、
- StringContent(字符串请求)、
- MultipartFormDataContent(Multipart 表单请求,一般带上传文件信息)、
- StreamContent(流内容)。
发送一个Get请求
private async void button1_Click(object sender, EventArgs e)
{
//发送异步的Get请求
HttpClient hc = new HttpClient();
//await hc.GetByteArrayAsync()
//await hc.GetStreamAsync()
string html=await hc.GetStringAsync("http://127.0.0.1:8081/Home/Login");
textBox1.Text = html;
}
发送一个POST表单格式请求:
private async void button2_Click(object sender, EventArgs e)
{
HttpClient hc = new HttpClient();
//List<KeyValuePair<string, string>> nameValues = new List<KeyValuePair<string, string>>();
//nameValues.Add(new KeyValuePair<string, string>("userName", "admin123"));
//nameValues.Add(new KeyValuePair<string, string>("password", "123123123"));
//Dictionary也是实现了IEnumerable接口的,所以也可以这样传值
Dictionary<string, string> nameValues = new Dictionary<string, string>();
nameValues["userName"] = "admin123";
nameValues["password"] = "";
FormUrlEncodedContent content = new FormUrlEncodedContent(nameValues);
HttpResponseMessage msg= await hc.PostAsync("http://127.0.0.1:8081/Home/Login", content);
//msg.Content;返回的Http报问题
//msg.Headers;获得响应头
//msg.StatusCode;获得响应的状态码 }
发送一个POST字符串请求:
private async void button3_Click(object sender, EventArgs e)
{
string json = "{userName:'admin',password:'123'}";
HttpClient client = new HttpClient();
StringContent content = new StringContent(json);
//contentype 设置协议类型,必不可少
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var respMsg = await client.PostAsync("http://127.0.0.1:6666/Home/Login2/", content);
string msgBody = await respMsg.Content.ReadAsStringAsync();
}
发送一个POST,Multipart 表单上传请求:
private async void button4_Click(object sender, EventArgs e)
{
HttpClient client = new HttpClient();
MultipartFormDataContent content = new MultipartFormDataContent();
content.Headers.Add("UserName", "admin");
content.Headers.Add("Password", "");
//先读取文件
using (Stream stream = File.OpenRead(@"D:\temp\xx.png"))
{
// 放入流中 上传接口协议名file 文件名
content.Add(new StreamContent(stream), "file", "logo.png");
var respMsg = await client.PostAsync("http://127.0.0.1:6666/Home/Upload/", content);
string msgBody = await respMsg.Content.ReadAsStringAsync();
MessageBox.Show(respMsg.StatusCode.ToString());
MessageBox.Show(msgBody);
}
}
多线程之HttpClient的更多相关文章
- Android-——多线程之Handler(转)
Android--多线程之Handler 原文地址:http://www.cnblogs.com/shirley-1019/p/3557800.html 前言 Android的消息传递机制是另外一种形 ...
- iOS多线程之8.NSOPeration的其他用法
本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...
- python 线程之 threading(四)
python 线程之 threading(三) http://www.cnblogs.com/someoneHan/p/6213100.html中对Event做了简单的介绍. 但是如果线程打算一遍一遍 ...
- python 线程之 threading(三)
python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...
- python 线程之_thread
python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...
- [深入浅出WP8.1(Runtime)]网络编程之HttpClient类
12.2 网络编程之HttpClient类 除了可以使用HttpWebRequest类来实现HTTP网络请求之外,我们还可以使用HttpClient类来实现.对于基本的请求操作,HttpClient类 ...
- Java多线程之ConcurrentSkipListMap深入分析(转)
Java多线程之ConcurrentSkipListMap深入分析 一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下, ...
- 【C#】线程之Parallel
在一些常见的编程情形中,使用任务也许能提升性能.为了简化变成,静态类System.Threading.Tasks.Parallel封装了这些常见的情形,它内部使用Task对象. Parallel.Fo ...
- iOS多线程之GCD小记
iOS多线程之GCD小记 iOS多线程方案简介 从各种资料中了解到,iOS中目前有4套多线程的方案,分别是下列4中: 1.Pthreads 这是一套可以在很多操作系统上通用的多线程API,是基于C语言 ...
随机推荐
- FormCollection获取请求数据
public ActionResult Add(FormCollection fm) //通过FormCollection 对象获取表单数据 { string message = "&quo ...
- sqlserver where in 在 mysql
) tmp); 主句(select * from (从句 temp) sql的 where in 删除 要更改为 // in( select * from ((select idfrom twhe ...
- drf02 序列化器详解 Serializer
作用 1. 序列化,序列化器会把模型对象转换成字典,经过response以后变成json字符串2. 反序列化,把客户端发送过来的数据,经过request以后变成字典,序列化器可以把字典转成模型3. 反 ...
- [Java]链表的打印,反转与删除
class Node{ public int value; public Node next=null; public Node(int value) { this.value=value; } }p ...
- python 疑难杂症
1.getpass模块 :Pycharm不支持getpass模块,使用terminal可运行,但是getpass中文提示显示乱码?
- loadrunner录制不了浏览器
Loadrunner11.0启动WebTours之总结1 第一次安装LR11时,安装安组件后没有对电脑进行重启,直接安装的LR112 安装完毕LR后,录制脚本时发现不能启动IE11.百度发现LR支持I ...
- 被遗忘的 Logrotate
转自: http://huoding.com/2013/04/21/246 被遗忘的 Logrotate 发表于 2013-04-21 我发现很多人的服务器上都运行着一些诸如每天切分 Nginx 日志 ...
- cent OS官网上下载老版本系统镜像的正确打开方式
当时的情况是这样的: 客户需要给服务器安装cent OS 7.3操作系统,我打开官网https://www.centos.org/,点击“GET CENTOS”——>“Minimal ISO”, ...
- Apache vs. Nginx
精简版 Apache:出名比较早,09年左右是最流行的时期,功能强大,可以根据需求配置为基于进程,基于线程或者基于事件的,但是消耗内存较多,对硬件需求较高,内存是影响服务器性能的最关键因素,在VPS上 ...
- HTML【2】表单提交与服务层的模拟(具体解释get与post提交方式的不同)
在HTML[1]中已经说明了HTML编程的基本方式,最后说到了表单提交的方式有get和post方式.那么究竟什么是get/post 方式呢,两者有什么差别?如今我就详细的介绍一下. 首先回想一下表单的 ...