You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
11 KiB
C#

12 months ago
using System.Net;
4 months ago
using System.Net.Http.Headers;
using System.Net.Http;
12 months ago
using System.Text;
4 months ago
using DS.Module.Core.Extensions;
using System.Security.Policy;
12 months ago
namespace DS.Module.Core.Helpers;
/// <summary>
/// 请求帮助类
/// </summary>
public class RequestHelper
4 months ago
{
public static string Post(string postData, string Url,int timeout = 60000)
12 months ago
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
req.Method = "POST";
req.Timeout = timeout; //设置请求超时时间,单位为毫秒
12 months ago
req.ContentType = "application/json";
byte[] data = Encoding.UTF8.GetBytes(postData);
req.ContentLength = data.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream stream = response.GetResponseStream();
Encoding encode = Encoding.UTF8;
StreamReader reader = new StreamReader(stream, encode);
string content = reader.ReadToEnd();
stream.Close();
reader.Close();
return content;
}
public static string Get(string Url)
{
string content = String.Empty;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(Url);
req.Method = "GET";
req.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream stream = response.GetResponseStream();
Encoding encode = Encoding.UTF8;
StreamReader reader = new StreamReader(stream, encode);
content = reader.ReadToEnd();
stream.Close();
reader.Close();
return content;
}
public static string GetResponseByGet(string url)
{
string result = string.Empty;
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = httpClient.GetAsync(url).Result;
//statusCode = response.StatusCode.ToString();
if (response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result;
}
}
return result;
}
4 months ago
/// <summary>
/// http post请求
/// </summary>
/// <param name="url">地址</param>
/// <param name="parameter">入参</param>
/// <param name="token"></param>
/// <param name="headers">头信息</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
/// , string token ="", Dictionary<string, string> headers = null
public static async Task<string> PostAsyncNoHeaders(string url, string parameter)
{
using (var client = new HttpClient())
{
//var data = new StringContent("param1=value1&param2=value2", Encoding.UTF8, "application/x-www-form-urlencoded");
var data = new StringContent(parameter, Encoding.UTF8, "application/x-www-form-urlencoded");
//_logger.LogWarning($"data:{data.ToJsonString()}parameter{parameter}");
// 添加header参数
//client.DefaultRequestHeaders.Add("Authorization", "Bearer your_token_here");
//foreach (var header in headers)
//{
// client.DefaultRequestHeaders.Add(header.Key, header.Value);
//}
//_logger.LogWarning($"header:{client.DefaultRequestHeaders.ToJsonString()}");
try
{
var response = await client.PostAsync(url, data);
response.EnsureSuccessStatusCode();
12 months ago
4 months ago
var responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
4 months ago
catch (HttpRequestException e)
{
throw new Exception(e.Message);
}
}
}
/// <summary>
/// http post请求
/// </summary>
/// <param name="url">地址</param>
/// <param name="parameter">入参</param>
/// <param name="token"></param>
/// <param name="headers">头信息</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
/// , string token ="", Dictionary<string, string> headers = null
public static async Task<string> PostJosnAsyncNoHeaders(string url, string parameter)
{
using (var client = new HttpClient())
{
//var data = new StringContent("param1=value1&param2=value2", Encoding.UTF8, "application/x-www-form-urlencoded");
var data = new StringContent(parameter, Encoding.UTF8, "application/json");
//_logger.LogWarning($"data:{data.ToJsonString()}parameter{parameter}");
// 添加header参数
//client.DefaultRequestHeaders.Add("Authorization", "Bearer your_token_here");
//foreach (var header in headers)
//{
// client.DefaultRequestHeaders.Add(header.Key, header.Value);
//}
//_logger.LogWarning($"header:{client.DefaultRequestHeaders.ToJsonString()}");
try
{
var response = await client.PostAsync(url, data);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
4 months ago
catch (HttpRequestException e)
{
throw new Exception(e.Message);
}
}
}
public static async Task<string> PostJosnAsyncByToken(string url, string parameter,string token)
{
using (var client = new HttpClient())
{
//var data = new StringContent("param1=value1&param2=value2", Encoding.UTF8, "application/x-www-form-urlencoded");
var data = new StringContent(parameter, Encoding.UTF8, "application/json");
//_logger.LogWarning($"data:{data.ToJsonString()}parameter{parameter}");
//添加header参数
client.DefaultRequestHeaders.Add("Authorization", "Bearer "+ token);
//foreach (var header in headers)
//{
// client.DefaultRequestHeaders.Add(header.Key, header.Value);
//}
//_logger.LogWarning($"header:{client.DefaultRequestHeaders.ToJsonString()}");
try
{
var response = await client.PostAsync(url, data);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
4 months ago
catch (HttpRequestException e)
{
throw new Exception(e.Message);
}
}
}
12 months ago
/// <summary>
/// http异步请求
/// </summary>
/// <param name="url">url</param>
/// <param name="reqMethod">请求方法 GET、POST</param>
/// <param name="callback">回调函数</param>
/// <param name="ob">回传对象</param>
/// <param name="postData">post数据</param>
public static void HttpAsyncRequest(string url, string reqMethod, AsyRequetCallback callback, object ob = null,
string postData = "")
{
Stream requestStream = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = reqMethod;
if (reqMethod.ToUpper() == "POST")
{
byte[] bytes = Encoding.UTF8.GetBytes(postData);
request.ContentLength = bytes.Length;
requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
}
//开始调用异步请求
//AsyResultTag 是自定义类 用于传递调用时信息 其中HttpWebRequest 是必须传递对象。
//因为回调需要用HttpWebRequest来获取HttpWebResponse
request.BeginGetResponse(new AsyncCallback(HttpCallback),
new AsyResultTag() { obj = ob, callback = callback, req = request });
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (requestStream != null)
{
requestStream.Close();
}
}
}
/// <summary>
/// http请求回调 由.net内部调用 参数必须为IAsyncResult
/// </summary>
/// <param name="asynchronousResult">http回调时回传对象</param>
private static void HttpCallback(IAsyncResult asynchronousResult)
{
int statusCode = 0;
string retString = "";
AsyResultTag tag = new AsyResultTag();
WebException webEx = null;
try
{
//获取请求时传递的对象
tag = asynchronousResult.AsyncState as AsyResultTag;
HttpWebRequest req = tag.req;
//获取异步返回的http结果
HttpWebResponse response = req.EndGetResponse(asynchronousResult) as HttpWebResponse;
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
statusCode = ((int)response.StatusCode);
}
catch (WebException ex)
{
if ((HttpWebResponse)ex.Response != null)
{
statusCode = ((int)((HttpWebResponse)ex.Response).StatusCode);
}
webEx = ex;
}
//调用外部回调 即最外层的回调
tag.callback(tag.obj, retString, statusCode, webEx);
}
/// <summary>
/// 异步请求回调委托
/// </summary>
/// <param name="asyObj">回传对象</param>
/// <param name="resStr">http响应结果</param>
/// <param name="statusCode">http状态码</param>
/// <param name="webEx">异常</param>
public delegate void AsyRequetCallback(object asyObj, string respStr, int statusCode, WebException webEx);
/// <summary>
/// 异步返回对象
/// </summary>
public class AsyResultTag
{
/// <summary>
/// 回传对象
/// </summary>
public object obj { get; set; }
/// <summary>
/// 当前httpRequest请求实例
/// </summary>
public HttpWebRequest req { get; set; }
/// <summary>
/// 回调函数委托
/// </summary>
public AsyRequetCallback callback { get; set; }
}
}