using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using DS.Module.Core.ServiceExtensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.Extensions.DependencyInjection;
namespace DS.Module.Core;
///
/// HTTP工具类
///
public static class HttpNewUtil
{
///
/// Http上下文访问器
///
public static IHttpContextAccessor HttpContextAccessor=> ServiceLocator.Instance.GetRequiredService();
///
/// 当前Http上下文
///
public static HttpContext HttpContext => HttpContextAccessor?.HttpContext;
// ///
// /// 初始化Web操作
// ///
// public static void Configure (IHttpContextAccessor _accessor) {
// try {
// HttpContextAccessor = _accessor;
// }
// catch {
// }
// }
///
/// 客户端IP地址
///
public static string Ip
{
get
{
var result = string.Empty;
if (HttpContext != null)
{
result = GetWebClientIp();
}
if (string.IsNullOrEmpty(result))
{
result = GetLanIp();
}
return result;
}
}
///
/// 得到客户端IP地址
///
///
private static string GetWebClientIp()
{
var ip = GetWebRemoteIp();
foreach (var hostAddress in Dns.GetHostAddresses(ip))
{
if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
{
return hostAddress.ToString();
}
}
return string.Empty;
}
///
/// 得到局域网IP地址
///
///
public static string GetLanIp()
{
foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
{
return hostAddress.ToString();
}
}
return string.Empty;
}
///
/// 得到远程Ip地址
///
///
private static string GetWebRemoteIp()
{
if (HttpContext?.Connection?.RemoteIpAddress == null)
return string.Empty;
var ip = HttpContext?.Connection?.RemoteIpAddress.ToString();
if (HttpContext == null)
return ip;
if (HttpContext.Request.Headers.ContainsKey("X-Real-IP"))
{
ip = HttpContext.Request.Headers["X-Real-IP"].ToString();
}
if (HttpContext.Request.Headers.ContainsKey("X-Forwarded-For"))
{
ip = HttpContext.Request.Headers["X-Forwarded-For"].ToString();
}
return ip;
}
///
/// 请求UserAgent信息
///
public static string UserAgent
{
get
{
string userAgent = HttpContext?.Request?.Headers["User-Agent"];
return userAgent;
}
}
///
/// 请求Url
///
public static string Url
{
get
{
var url = new StringBuilder().Append(HttpContext?.Request?.Scheme).Append("://")
.Append(HttpContext?.Request?.Host).Append(HttpContext?.Request?.PathBase)
.Append(HttpContext?.Request?.Path).Append(HttpContext?.Request?.QueryString).ToString();
return url;
}
}
///
/// 得到操作系统版本
///
///
public static string GetOSVersion()
{
var osVersion = string.Empty;
var userAgent = UserAgent;
if (userAgent.Contains("NT 10"))
{
osVersion = "Windows 10";
}
else if (userAgent.Contains("NT 6.3"))
{
osVersion = "Windows 8";
}
else if (userAgent.Contains("NT 6.1"))
{
osVersion = "Windows 7";
}
else if (userAgent.Contains("NT 6.0"))
{
osVersion = "Windows Vista/Server 2008";
}
else if (userAgent.Contains("NT 5.2"))
{
osVersion = "Windows Server 2003";
}
else if (userAgent.Contains("NT 5.1"))
{
osVersion = "Windows XP";
}
else if (userAgent.Contains("NT 5"))
{
osVersion = "Windows 2000";
}
else if (userAgent.Contains("NT 4"))
{
osVersion = "Windows NT4";
}
else if (userAgent.Contains("Android"))
{
osVersion = "Android";
}
else if (userAgent.Contains("Me"))
{
osVersion = "Windows Me";
}
else if (userAgent.Contains("98"))
{
osVersion = "Windows 98";
}
else if (userAgent.Contains("95"))
{
osVersion = "Windows 95";
}
else if (userAgent.Contains("Mac"))
{
osVersion = "Mac";
}
else if (userAgent.Contains("Unix"))
{
osVersion = "UNIX";
}
else if (userAgent.Contains("Linux"))
{
osVersion = "Linux";
}
else if (userAgent.Contains("SunOS"))
{
osVersion = "SunOS";
}
return osVersion;
}
}