C# 获取源代码
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Net;
11 using System.Text;
12 using System.IO;
13
14 public partial class _Default : System.Web.UI.Page
15 {
16 /// <summary>
17 /// 塗聚文
18 /// 涂聚文
19 /// </summary>
20 /// <param name="sender"></param>
21 /// <param name="e"></param>
22 protected void Page_Load(object sender, EventArgs e)
23 {
24 //获取URL地址
25 string url = Request.Url.ToString();
26 txtUrl.Text = url;
27 //获取IP地址
28 string ips= Request.UserHostAddress.ToString();
29 txtIp.Text = ips;
30 //浏览器
31 string fr = Request.Browser.Type.ToString();
32 txtbrows.Text = fr;
33 string cp ;//= Request.UserAgent.ToString()
34 //操作系统
35 cp=GetOSNameByUserAgent(Request.UserAgent.ToString());
36 txtcp.Text = cp;
37 //浏览器版本号
38 //this.txtbrows.Text = Request.Browser.ClrVersion.ToString();
39 // txtUrl.Text = Request.ServerVariables["http_referer"];
40 }
41
42 protected void btnGet_Click(object sender, EventArgs e)
43 {
44 txtUrl.Text = Request.ServerVariables["http_referer"];//ServerViables["http_referer"];
45 string strurl= txtUrl.Text.ToString(); //欲获取的网页地址 要 http://
46 WebClient myWebClient = new WebClient(); //创建WebClient实例myWebClient
47
48 //获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
49 myWebClient.Credentials = CredentialCache.DefaultCredentials;
50 // Request.ServerVariables("HTTP_HOST");
51 //从资源下载数据并返回字节数组。(加@是因为网址中间有"/"符号)
52 byte[] pagedata = myWebClient.DownloadData(@strurl);
53
54 string result = Encoding.Default.GetString(pagedata); //如果获取网站页面采用的是GB2312,则使用这句
55 //string result = Encoding.UTF8.GetString(pagedata); //如果获取网站页面采用的是UTF-8,则使用这句
56 Response.Write(result); //在WEB页中显示获取的内容
57 Panel1.Visible = false;
58
59 }
60 /// <summary>
61 /// 根据 User Agent 获取操作系统名称
62 /// </summary>
63 private string GetOSNameByUserAgent(string userAgent)
64 {
65 string osVersion = "未知";
66 if (userAgent.Contains("NT 6.2"))
67 {
68 osVersion = "Windows 8/Windows Server 2012";
69 }
70 else if (userAgent.Contains("NT 6.1"))
71 {
72 osVersion = "Windows 7/Windows Server 2008 R2";
73 }
74 else if (userAgent.Contains("NT 6.0"))
75 {
76 osVersion = "Windows Vista/Server 2008";
77 }
78 else if (userAgent.Contains("NT 5.2"))
79 {
80 osVersion = "Windows Server 2003";
81 }
82 else if (userAgent.Contains("NT 5.1"))
83 {
84 osVersion = "Windows XP";
85 }
86 else if (userAgent.Contains("NT 5"))
87 {
88 osVersion = "Windows 2000";
89 }
90 else if (userAgent.Contains("NT 4"))
91 {
92 osVersion = "Windows NT4";
93 }
94 else if (userAgent.Contains("Me"))
95 {
96 osVersion = "Windows Me";
97 }
98 else if (userAgent.Contains("98"))
99 {
100 osVersion = "Windows 98";
101 }
102 else if (userAgent.Contains("95"))
103 {
104 osVersion = "Windows 95";
105 }
106 else if (userAgent.Contains("Mac"))
107 {
108 osVersion = "Mac";
109 }
110 else if (userAgent.Contains("Unix"))
111 {
112 osVersion = "UNIX";
113 }
114 else if (userAgent.Contains("Linux"))
115 {
116 osVersion = "Linux";
117 }
118 else if (userAgent.Contains("SunOS"))
119 {
120 osVersion = "SunOS";
121 }
122 return osVersion;
123 }
124 /// <summary>
125
126 /// 获取源代码
127
128 /// </summary>
129
130 /// <param name="url"></param>
131
132 /// <param name="encoding"></param>
133
134 /// <returns></returns>
135
136 public static string GetPage(string url, Encoding encoding)
137 {
138
139 HttpWebRequest request = null;
140
141 HttpWebResponse response = null;
142
143 StreamReader reader = null;
144
145 try
146 {
147
148 request = (HttpWebRequest)WebRequest.Create(url);
149
150 request.UserAgent = "www.dupcit.com";
151
152 request.Timeout = 20000;
153
154 request.AllowAutoRedirect = false;
155
156
157
158 response = (HttpWebResponse)request.GetResponse();
159
160 if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
161 {
162
163 reader = new StreamReader(response.GetResponseStream(), encoding);
164
165 string html = reader.ReadToEnd();
166
167
168
169 return html;
170
171 }
172
173 }
174
175 catch
176 {
177
178 }
179
180 finally
181 {
182
183
184
185 if (response != null)
186 {
187
188 response.Close();
189
190 response = null;
191
192 }
193
194 if (reader != null)
195
196 reader.Close();
197
198
199
200 if (request != null)
201
202 request = null;
203
204
205
206 }
207
208
209
210 return string.Empty;
211
212 }
213 }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)