using System.Text;
using DS.Module.Core.Extensions;
using Microsoft.AspNetCore.Http;
namespace DS.Module.Core;
///
///
///
public static class HttpUtil
{
public static string GetClientIP(HttpContext context)
{
var ip = context.Request.Headers["X-Forwarded-For"].ObjToString();
if (string.IsNullOrEmpty(ip))
{
ip = context.Connection.RemoteIpAddress.ObjToString();
}
return ip;
}
///
/// 获取响应内容
///
///
///
public static async Task GetResponse(HttpResponse response)
{
response.Body.Seek(0, SeekOrigin.Begin);
var text = await new StreamReader(response.Body).ReadToEndAsync();
response.Body.Seek(0, SeekOrigin.Begin);
return text;
}
//
// 摘要:
// 获取完整请求地址
//
// 参数:
// request:
public static string GetRequestUrlAddress(HttpRequest request)
{
return new StringBuilder().Append(request.Scheme).Append("://").Append(request.Host)
.Append(request.PathBase)
.Append(request.Path)
.Append(request.QueryString)
.ToString();
}
}