程序模拟HTTP请求
2016-05-09 10:23
651 查看
1. 使用HttpClient
前面拼接StringContent
使用await:
这里可判断请求的状态
View Code
前面拼接StringContent
string strContent = "client_id=client&client_secret=secret&grant_type=client_credentials"; HttpContent content = new StringContent(strContent, Encoding.UTF8, "application/x-www-form-urlencoded");
使用await:
public async void GetResponse() { HttpClient client = new HttpClient(); Url = "http://xxxx"; ClientInfo info = new ClientInfo(); Json = JsonConvert.SerializeObject(info, Settings); HttpContent content = new StringContent(Json, Encoding.UTF8, "application/json"); using (var response = await client.PostAsync(Url, content)) { var result = await response.Content.ReadAsStringAsync(); Console.WriteLine(result); } }
这里可判断请求的状态
using (HttpClient client = new HttpClient()) { using (var response = client.GetAsync(url)) { response.Result.EnsureSuccessStatusCode(); var name = response.Result.Content.Headers.ContentDisposition.FileName.Replace("\"", ""); var buff = response.Result.Content.ReadAsByteArrayAsync().Result; string filename = Path.Combine(path, name); using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate)) { fs.Write(buff, 0, buff.Length); } } }
View Code
相关文章推荐
- Spring HTTP Invoker使用介绍
- TCP/IP详解学习笔记(5)-IP选路,动态选路,和一些细节
- linux tcpdump
- NAT网络下TCP连接建立时可能SYN包被服务器忽略-tcp_tw_recycle
- TCP/IP详解学习笔记(4)-ICMP协议,ping和Traceroute
- 【网络编程/C++】修改本机ip地址
- 中间人攻击实验 https
- TCP/IP详解学习笔记(3)-IP协议,ARP协议,RARP协议
- ios网络学习-- UIWebView的加载本地数据的三种方式
- linux-tcpdump抓包-邮件加密配置-nmap扫描-tcpwrap的使用
- 当约架成为一门生意 游戏还可以这样创新
- 如何防止运营商网络劫持,避免被他人强行插入广告?
- TCP/IP详解学习笔记(2)-数据链路层
- TCP实现多个客户端与服务端 数据 传输
- TCP/IP详解学习笔记(1)-基本概念
- Nodejs获取网络数据并生成Excel表格
- Retrofit+OKHttp 教你怎么持久化管理Cookie
- WPA-PSK无线网络破解原理及过程(转)
- chrome浏览器自带的开发者工具查看http头
- Linux下编译安装httpd-2.2.31