aspx net.2.0 C#获取IP,URL,浏览器,操作系统
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Net;
11using System.Text;
12using System.IO;
13
14public partial class _Default : System.Web.UI.Page
15{
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 //获取URL地址
19 string url = Request.Url.ToString();
20 txtUrl.Text = url;
21 //获取IP地址
22 string ips= Request.UserHostAddress.ToString();
23 txtIp.Text = ips;
24 //浏览器
25 string fr = Request.Browser.Type.ToString();
26 txtbrows.Text = fr;
27 string cp ;//= Request.UserAgent.ToString()
28 //操作系统
29 cp=GetOSNameByUserAgent(Request.UserAgent.ToString());
30 txtcp.Text = cp;
31 //浏览器版本号
32 //this.txtbrows.Text = Request.Browser.ClrVersion.ToString();
33 // txtUrl.Text = Request.ServerVariables["http_referer"];
34 }
35
36 protected void btnGet_Click(object sender, EventArgs e)
37 {
38 txtUrl.Text = Request.ServerVariables["http_referer"];//ServerViables["http_referer"];
39 string strurl= txtUrl.Text.ToString(); //欲获取的网页地址 要 http://
40 WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient
41
42 //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
43 myWebClient.Credentials = CredentialCache.DefaultCredentials;
44 // Request.ServerVariables("HTTP_HOST");
45 //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)
46 byte[] pagedata = myWebClient.DownloadData(@strurl);
47
48 string result = Encoding.Default.GetString(pagedata); //如果获取网站页面采用的是GB2312,则使用这句
49 //string result = Encoding.UTF8.GetString(pagedata); //如果获取网站页面采用的是UTF-8,则使用这句
50 Response.Write(result); //在WEB页中显示获取的内容
51 Panel1.Visible = false;
52
53 }
54 /// <summary>
55 /// 根据 User Agent 获取操作系统名称
56 /// </summary>
57 private string GetOSNameByUserAgent(string userAgent)
58 {
59 string osVersion = "未知";
60
61 if (userAgent.Contains("NT 6.0"))
62 {
63 osVersion = "Windows Vista/Server 2008";
64 }
65 else if (userAgent.Contains("NT 5.2"))
66 {
67 osVersion = "Windows Server 2003";
68 }
69 else if (userAgent.Contains("NT 5.1"))
70 {
71 osVersion = "Windows XP";
72 }
73 else if (userAgent.Contains("NT 5"))
74 {
75 osVersion = "Windows 2000";
76 }
77 else if (userAgent.Contains("NT 4"))
78 {
79 osVersion = "Windows NT4";
80 }
81 else if (userAgent.Contains("Me"))
82 {
83 osVersion = "Windows Me";
84 }
85 else if (userAgent.Contains("98"))
86 {
87 osVersion = "Windows 98";
88 }
89 else if (userAgent.Contains("95"))
90 {
91 osVersion = "Windows 95";
92 }
93 else if (userAgent.Contains("Mac"))
94 {
95 osVersion = "Mac";
96 }
97 else if (userAgent.Contains("Unix"))
98 {
99 osVersion = "UNIX";
100 }
101 else if (userAgent.Contains("Linux"))
102 {
103 osVersion = "Linux";
104 }
105 else if (userAgent.Contains("SunOS"))
106 {
107 osVersion = "SunOS";
108 }
109 return osVersion;
110 }
111}
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Net;
11using System.Text;
12using System.IO;
13
14public partial class _Default : System.Web.UI.Page
15{
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 //获取URL地址
19 string url = Request.Url.ToString();
20 txtUrl.Text = url;
21 //获取IP地址
22 string ips= Request.UserHostAddress.ToString();
23 txtIp.Text = ips;
24 //浏览器
25 string fr = Request.Browser.Type.ToString();
26 txtbrows.Text = fr;
27 string cp ;//= Request.UserAgent.ToString()
28 //操作系统
29 cp=GetOSNameByUserAgent(Request.UserAgent.ToString());
30 txtcp.Text = cp;
31 //浏览器版本号
32 //this.txtbrows.Text = Request.Browser.ClrVersion.ToString();
33 // txtUrl.Text = Request.ServerVariables["http_referer"];
34 }
35
36 protected void btnGet_Click(object sender, EventArgs e)
37 {
38 txtUrl.Text = Request.ServerVariables["http_referer"];//ServerViables["http_referer"];
39 string strurl= txtUrl.Text.ToString(); //欲获取的网页地址 要 http://
40 WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient
41
42 //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
43 myWebClient.Credentials = CredentialCache.DefaultCredentials;
44 // Request.ServerVariables("HTTP_HOST");
45 //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)
46 byte[] pagedata = myWebClient.DownloadData(@strurl);
47
48 string result = Encoding.Default.GetString(pagedata); //如果获取网站页面采用的是GB2312,则使用这句
49 //string result = Encoding.UTF8.GetString(pagedata); //如果获取网站页面采用的是UTF-8,则使用这句
50 Response.Write(result); //在WEB页中显示获取的内容
51 Panel1.Visible = false;
52
53 }
54 /// <summary>
55 /// 根据 User Agent 获取操作系统名称
56 /// </summary>
57 private string GetOSNameByUserAgent(string userAgent)
58 {
59 string osVersion = "未知";
60
61 if (userAgent.Contains("NT 6.0"))
62 {
63 osVersion = "Windows Vista/Server 2008";
64 }
65 else if (userAgent.Contains("NT 5.2"))
66 {
67 osVersion = "Windows Server 2003";
68 }
69 else if (userAgent.Contains("NT 5.1"))
70 {
71 osVersion = "Windows XP";
72 }
73 else if (userAgent.Contains("NT 5"))
74 {
75 osVersion = "Windows 2000";
76 }
77 else if (userAgent.Contains("NT 4"))
78 {
79 osVersion = "Windows NT4";
80 }
81 else if (userAgent.Contains("Me"))
82 {
83 osVersion = "Windows Me";
84 }
85 else if (userAgent.Contains("98"))
86 {
87 osVersion = "Windows 98";
88 }
89 else if (userAgent.Contains("95"))
90 {
91 osVersion = "Windows 95";
92 }
93 else if (userAgent.Contains("Mac"))
94 {
95 osVersion = "Mac";
96 }
97 else if (userAgent.Contains("Unix"))
98 {
99 osVersion = "UNIX";
100 }
101 else if (userAgent.Contains("Linux"))
102 {
103 osVersion = "Linux";
104 }
105 else if (userAgent.Contains("SunOS"))
106 {
107 osVersion = "SunOS";
108 }
109 return osVersion;
110 }
111}
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)