首页 > 其他分享 > HttpHelper

HttpHelper

时间:2023-04-12 12:02:54浏览次数:43  
标签:contentType string postData HttpHelper headers timeOut null

public class HttpHelpe
{
    public static async Task<T> GetAsync<T>(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
    {
        return await RequestAsync<T>(url, "GET", postData, contentType, timeOut, headers);
    }

    public static async Task<T> PostAsync<T>(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
    {
        return await RequestAsync<T>(url, "POST", postData, contentType, timeOut, headers);
    }

    public static async Task<T> PutAsync<T>(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
    {
        return await RequestAsync<T>(url, "PUT", postData, contentType, timeOut, headers);
    }

    public static async Task<T> DeleteAsync<T>(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
    {
        return await RequestAsync<T>(url, "DELETE", postData, contentType, timeOut, headers);
    }

    private static async Task<T> RequestAsync<T>(string url, string method, string postData = null, string contentType = null, int timeOut = 30,
        Dictionary<string, string> headers = null)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = method;
        request.Timeout = timeOut * 1000;

        if (!string.IsNullOrEmpty(contentType))
        {
            request.ContentType = contentType;
        }

        if (headers != null)
        {
            foreach (var header in headers)
            {
                request.Headers[header.Key] = header.Value;
            }
        }

        if (!string.IsNullOrEmpty(postData) && (method == "POST" || method == "PUT"))
        {
            byte[] dataBytes = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = dataBytes.Length;

            using (Stream stream = await request.GetRequestStreamAsync())
            {
                await stream.WriteAsync(dataBytes, 0, dataBytes.Length);
            }
        }

        using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
        {
            using (Stream stream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(stream);
                string responseBody = await reader.ReadToEndAsync();
                T responseObject = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseBody);
                return responseObject;
            }
        }
    }
}

 

标签:contentType,string,postData,HttpHelper,headers,timeOut,null
From: https://www.cnblogs.com/LZXX/p/17309333.html

相关文章

  • WebForm之企业微信开发(1)——准备httphelper
    usingSystem;usingSystem.IO;usingSystem.Net;usingSystem.Text;publicclassHttpHelper{///<summary>///发起一个HTTP请求(以POS......
  • C#实现HTTP访问类HttpHelper
    在项目开发过程中,我们经常会访问第三方接口,如我们需要接入的第三方接口是WebAPI,这时候我们就需要使用HttpHelper调用远程接口了。示例中的HttpHelper类使用Log4Net记录了......