C# 后台访问webapi
来源:互联网 发布:淘宝如何参加9.9包邮 编辑:程序博客网 时间:2024/05/16 06:51
具体在哪看见的忘记了。
public static class CallWebAPI { public static async Task<string> APIPost(string url, string data) { string result = string.Empty; //设置HttpClientHandler的AutomaticDecompression var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; //创建HttpClient(注意传入HttpClientHandler) using (var http = new HttpClient(handler)) { //使用FormUrlEncodedContent做HttpContent var content = new FormUrlEncodedContent(new Dictionary<string, string>() { {"", data}//键名必须为空 }); //await异步等待回应 var response = await http.PostAsync(url, content); //确保HTTP成功状态值 response.EnsureSuccessStatusCode(); //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip) result = await response.Content.ReadAsStringAsync(); } return result; } static async void APIGet(string url) { //创建HttpClient(注意传入HttpClientHandler) var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; using (var http = new HttpClient(handler)) { //await异步等待回应 var response = await http.GetAsync(url); //确保HTTP成功状态值 response.EnsureSuccessStatusCode(); //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip) Console.WriteLine(await response.Content.ReadAsStringAsync()); } } }
0 0
- C# 后台访问webapi
- WebApi(6) 后台C#调用WebApi
- webapi 跨域访问
- Android访问WEBAPI
- WebAPI 后台调用 采用httpclient
- C# WebApi 上传文件
- c#之webapi
- c# webapi 开发
- C# WebApi 上传文件
- c# 调用webApi
- C# webAPI的使用
- java 调用 C# webapi
- C# 调用WebApi
- C#调用webApi方法
- C# WebApi 上传文件
- C# WebAPI 传递可选参数及指定访问那个接口的方法。
- WebApi中实现Cors访问
- 允许WebApi跨域访问
- WebSocket 浅析
- 强化学习(二)
- Cannot change version of project facet Dynamic web module to 2.5
- Tomcat的接收、处理、返回请求原理及NIO应用浅析
- iOS 快速集成启动页广告
- C# 后台访问webapi
- Cannot create __weak reference in file using manual reference counting
- JQuery中表单验证相关的一些技巧
- C++中引用(&)的用法和应用实例
- the left operand of * is a garbage value
- github使用问题
- css实现多行文字与图片垂直水平居中
- 解决web.xml 报错The content of element type "web-app" must match
- 分布式与集群的区别