基于HttpClient封装的请求类
2021-04-08 23:25
标签:string ons request foreach sum for ada headers 序列化 using ICSharpCode.SharpZipLib.GZip; /// /// } /// HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false }); } /// /// 基于HttpClient封装的请求类 标签:string ons request foreach sum for ada headers 序列化 原文地址:https://www.cnblogs.com/chenboyhd/p/12454478.html
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
/// 基于HttpClient封装的请求类
///
public class HttpRequest
{
///
/// 使用post方法异步请求
///
/// 目标链接
/// 发送的参数字符串,只能用json
///
public static async Task
{
HttpClient client = new HttpClient();
HttpContent content = new StringContent(json);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
/// 使用post方法异步请求
///
/// 目标链接
/// 发送的参数字符串
///
public static async Task
{
HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false });
HttpContent content = new StringContent(data);
if (header != null)
{
client.DefaultRequestHeaders.Clear();
foreach (var item in header)
{
client.DefaultRequestHeaders.Add(item.Key, item.Value);
}
}
HttpResponseMessage response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
string responseBody = "";
if (Gzip)
{
GZipInputStream inputStream = new GZipInputStream(await response.Content.ReadAsStreamAsync());
responseBody = new StreamReader(inputStream).ReadToEnd();
}
else
{
responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
/// 使用get方法异步请求
///
/// 目标链接
///
public static async Task
{
if (header != null)
{
client.DefaultRequestHeaders.Clear();
foreach (var item in header)
{
client.DefaultRequestHeaders.Add(item.Key, item.Value);
}
}
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();//用来抛异常的
string responseBody = "";
if (Gzip)
{
GZipInputStream inputStream = new GZipInputStream(await response.Content.ReadAsStreamAsync());
responseBody = new StreamReader(inputStream).ReadToEnd();
}
else
{
responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
/// 使用post返回异步请求直接返回对象
///
///
///
/// 请求链接
/// 请求对象数据
///
public static async Task
{
String json = JsonConvert.SerializeObject(obj);
string responseBody = await PostAsyncJson(url, json); //请求当前账户的信息
return JsonConvert.DeserializeObject
}
/// 使用Get返回异步请求直接返回对象
///
///
/// 请求链接
///
public static async Task
{
string responseBody = await GetAsync(url); //请求当前账户的信息
return JsonConvert.DeserializeObject
}
}
————————————————
原文链接:https://blog.csdn.net/zanllp/article/details/85501932
上一篇:js的Set和Map集合
下一篇:阿里云上搭建https网站全流程
文章标题:基于HttpClient封装的请求类
文章链接:http://soscw.com/index.php/essay/73069.html