mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-20 02:29:24 +08:00
转移.net core 3.1,为.NET 5做准备
This commit is contained in:
70
Infrastructure/Utilities/HttpManager.cs
Normal file
70
Infrastructure/Utilities/HttpManager.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Infrastructure.Utilities
|
||||
{
|
||||
public class HttpManager
|
||||
{
|
||||
public static Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
request.Method = "POST";
|
||||
if (!string.IsNullOrEmpty(contentType))
|
||||
{
|
||||
request.ContentType = contentType;
|
||||
}
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (var header in headers)
|
||||
request.Headers[header.Key] = header.Value;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(postData ?? "");
|
||||
using (Stream sendStream = request.GetRequestStream())
|
||||
{
|
||||
sendStream.Write(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
||||
{
|
||||
Stream responseStream = response.GetResponseStream();
|
||||
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
|
||||
return streamReader.ReadToEndAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Task.FromResult(ex.Message);
|
||||
}
|
||||
|
||||
}
|
||||
public static Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
if (headers != null)
|
||||
{
|
||||
foreach (var header in headers)
|
||||
request.Headers[header.Key] = header.Value;
|
||||
}
|
||||
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
||||
{
|
||||
Stream responseStream = response.GetResponseStream();
|
||||
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
|
||||
return streamReader.ReadToEndAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Task.FromResult(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user