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.

50 lines
1.2 KiB
C#

using System.Text;
using DS.Module.Core.Extensions;
using Microsoft.AspNetCore.Http;
namespace DS.Module.Core;
/// <summary>
///
/// </summary>
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;
}
/// <summary>
/// 获取响应内容
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
public static async Task<string> 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();
}
}