多线程之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语言 ...
随机推荐
- 面试回答问题要防范hr的陷阱
面试技巧是大众化的,比较实在.在经历的各种面试中,最不容易防范的面试就是“拉呱”(山东方言,聊天的意思),这样子自己容易放松警惕. 看看下面的,就知道应该怎么应对这些“滑头”的HR! 1.我们为什么要 ...
- Model2
Model1: Model2:
- TCP协议的三次握手、四次挥手
TCP三次握手 TCP的连接的建立需要发送三个包,一次称为三次握手(Three-way Handshake). 三次握手的目的是连接服务器指定端口,建立TCP连接,并同步连接双方的序列号和确认号并交换 ...
- python tips:类的专有属性
实例通常能够调用类的属性,但是有些属性是类专有的,实例无法调用. 实例调用方法时查找属性时,首先在自己的__dict__中找,找不到去类中找,在类中能够找到的属性都位于dir(cls)中,如果类的某些 ...
- eas之f7
f7控件实际上是一张单据.所以对于数据的修改实际上是需要修改单据的,是在eas中修改单据的元数据是组件.包括了f7控件, F7是个快捷键,是某个字段符合条件的集合! F7就是一个控件,用来 ...
- C#第三节课(1)
数据类型 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System. ...
- #MySQL数据库无法远程访问的问题
在 Ubuntu上装了mysql,因为项目的数据库是mysql,将项目放在tomcat里面webapp下面,一直启动不成功.本来一直以为是jdbc驱动问题,后来发现不是. 感谢!!http://blo ...
- 突如其来的“中断异常”,我(Java)该如何处理?
3.try-catch块 try语句块中代码执行时发生三种情况: try语句块中代码正常执行完毕,没有任何异常,那么catch语句块的代码将不会被执行. import java.util.*; pub ...
- redis在windows上通过cmd连接服务器(需要密码)
- Netty学习总结(3)——Netty百万级推送服务
1. 背景 1.1. 话题来源 最近很多从事移动互联网和物联网开发的同学给我发邮件或者微博私信我,咨询推送服务相关的问题.问题五花八门,在帮助大家答疑解惑的过程中,我也对问题进行了总结,大概可以归纳为 ...