多线程之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语言 ...
随机推荐
- 基于 react-navigation 父子组件的跳转链接
1.在一个页面中中引入一个组件,但是这个组件是一个小组件,例如是一个cell,单独的每个cell都是需要点击有链接跳转的,这个时候通常直接使用 onPress 的跳转就会不起作用 正确的处理方法是,在 ...
- 【转】【Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之RAC 特殊问题和实战经验(五)
原文地址:http://www.cnblogs.com/baiboy/p/orc5.html 阅读目录 目录 共享存储 时间一致性 互联网络(或者私有网络.心跳线) 固件.驱动.升级包的一致性 共 ...
- Webpack 快速上手(下)
杏仁前端开发工程师,代码洁癖症早期,关注前端技术. 由于文章篇幅较长,为了更好的阅读体验,本文分为上.中.下三篇: 上篇介绍了什么是 webpack,为什么需要 webpack,webpack 的文件 ...
- 【转载】使用JSONObject生成和解析json
1. json数据类型 类型 描述 Number 数字型 String 字符串型 Boolean 布尔型 Array 数组 Object 对象 null 空值 (1)json中不区分整数.小数等类型, ...
- 【转载】servlet三大作用域:request,session,application
javaweb开发中Servlet三大域对象的应用(request.session.application(ServletContext)). 1. requestrequest是表示一个请求,只要发 ...
- softmax回归---sigmoid(1)
介绍softmax之前先讲讲sigmoid: 逻辑回归模型常用的函数:sigmoid函数(用来做二分类) 表达式:f(x)=L/(1+exp-k(x-x0)) 其图像: 本质:将一个真值映射到(0,1 ...
- 0823关于整理MySQL死锁
-- 更多的是需要理解 http://hedengcheng.com/?p=577 -- 何登成 关于WHERE的提取http://www.cnblogs.com/metoy/p/5545580.ht ...
- 收到微软寄来的MVP包裹
经过一年多的艰苦努力,最终在7月份获得了微软SharePoint的MVP. 今天收到了微软从美国寄来的包裹. 内部图: 奖牌: 回忆过去一年.白天和客户撕逼,晚上回家写博客,或者翻译英文文章,去论坛回 ...
- HDU 2586 How far away ?(LCA模板 近期公共祖先啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...
- Android布局文件经验
1.父控件中含有多个子控件时.往往遵循长子优先的原则,即长子假设非常大可能占满父空间.使次子们出局: 2.如果TableLayout有2行,当中一行未设定列间长度比例.而还有一行设定了,则未设定行可能 ...