zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.UI.HtmlControls;
using System.IO;
using System.IO.Compression;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections.Generic;
using System.Net;
 
namespace HH.WMS.Utils
{
    /// <summary>
    /// WEB请求上下文信息工具类
    /// </summary>
    public partial class ZHttp
    {
        #region 可以获取客户端上次请求的url的有关信息
        /// <summary>
        /// 返回上一个页面的地址
        /// </summary>
        /// <returns>上一个页面的地址</returns>
        public static string ReferrerUrl
        {
            get
            {
                return HttpContext.Current.Request.UrlReferrer == null ? string.Empty : HttpContext.Current.Request.UrlReferrer.OriginalString;
            }
        }
        #endregion
 
        #region 得到用户IP地址
        /// <summary>
        /// 得到用户IP地址
        /// </summary>
        /// <returns>返回用户IP地址,如果获取不到返回 0.0.0.0 </returns>
        /// 
        public static string ClientIP
        {
            get
            {
                var context = HttpContext.Current;
                string result = context.Request.UserHostAddress;
                if (string.IsNullOrEmpty(result))
                {
                    result = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];//获取包括使用了代理服务器的地址列表。
                }
                if (string.IsNullOrEmpty(result))
                {
                    result = context.Request.ServerVariables["REMOTE_ADDR"];//最后一个代理服务器地址。
                }
                if (string.IsNullOrEmpty(result))
                {
                    result = context.Request.UserHostAddress;
                }
                return result;
            }
        }
        #endregion
 
        public static bool IsLanIP(string IP)
        {
            var result = false;
            var ips = new List<string>() { "10.", "192.", "172.", "127.", "::1", "localhost" };
            ips.ForEach(x=>{
                if (IP.StartsWith(x)) result = true;
            });
            return result;
        }
 
        public static string ClientHostName 
        {
            get 
            {
                var context = HttpContext.Current;
                string IP = context.Request.UserHostAddress;
                string Name = context.Request.UserHostName;
 
                if (Name == IP || Name == "127.0.0.1" || Name == "::1")
                {
                    Name = context.Request.ServerVariables["REMOTE_HOST"];
                }
                if (Name == IP || Name == "127.0.0.1" || Name == "::1")
                {
                    try { Name = Dns.GetHostEntry(IP).HostName; }
                    catch { }
                }
 
                return Name;
            }
        }
 
        #region 判断是否来自搜索引擎链接
        /// <summary>
        /// 判断是否来自搜索引擎链接
        /// </summary>
        /// <returns>是否来自搜索引擎链接</returns>
        public static bool IsFromSearchEngines
        {
            get
            {
                string[] SearchEngine = { "google", "yahoo", "msn", "baidu", "sogou", "sohu", "sina", "163", "lycos", "tom", "yisou", "iask", "soso", "gougou", "zhongsou" };
                if (HttpContext.Current.Request.UrlReferrer != null)
                {
                    string tmpReferrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLower();
                    for (int i = 0; i < SearchEngine.Length; i++)
                    {
                        if (tmpReferrer.Contains(SearchEngine[i]))
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
        }
        #endregion
 
        #region 取得客户端的操作系统
        public static string GetPlatformName(HttpRequestBase request)
        {
            string userAgent = request.UserAgent;
 
            if (string.IsNullOrEmpty(userAgent))
                return "未知类型";
 
            else if (userAgent.IndexOf("Windows NT 6.2") != -1)
                return "Windows 8";
 
            else if (userAgent.IndexOf("Windows NT 6.1") != -1)
                return "Windows 7";
 
            else if (userAgent.IndexOf("Windows NT 6") != -1)
                return "Windows Vista";
 
            else if (userAgent.IndexOf("Windows NT 5.1") != -1)
                return "Windows XP";
 
            else if (userAgent.IndexOf("Windows NT 5.2") != -1)
                return "Windows Server 2003";
 
            else if (userAgent.IndexOf("Windows NT 5") != -1)
                return "Windows 2000";
 
            else if (userAgent.IndexOf("iPhone") != -1)
                return "iPhone";
 
            else if (userAgent.IndexOf("(iPad;") != -1)
                return "iPad";
 
            else if (userAgent.IndexOf("Android") != -1)
                return "Android";
 
            else if (userAgent.IndexOf("9x") != -1)
                return "Windows ME";
 
            else if (userAgent.IndexOf("98") != -1)
                return "Windows 98";
 
            else if (userAgent.IndexOf("95") != -1)
                return "Windows 95";
 
            else if (userAgent.IndexOf("NT 4") != -1)
                return "Windows NT 4";
 
            if (request.Browser != null && !string.IsNullOrEmpty(request.Browser.Platform))
                return request.Browser.Platform.Replace("WinCE", "Windows CE");
            else
                return "未知类型";
        }
        #endregion
    }
}