- 热门文章:
- · asp.net中使用静态变量
- · 利用HttpRequest登录到某个网站,然后获取网站信息的程序示例 [原创]
- · Creating DataGrid Templated Columns Dynamically - Part II(转自DotNetTips)
- · 用ASP.NET写你自己的代码生成器(2)。
- · 用ASP.NET写你自己的代码生成器(3)。
- · mark新官上任,转贴一个DataGrid(增加删除确认和新增记录功能),道贺:)
- · 一个datagrid 删除确认例子
- · 关于ASP.Net不能启动调试的官方解答
- · 在WEB窗体中如何转换页面和结束程序?
- · 关于ASP.Net写注册表权限问题的官方解决方法
- · 动态生成柱状图
- · 一个SDK里做聊天室的例子(2)
上一篇:配置Config.web >>
WebRequest Class
Author | Date of Submission | User Level |
Kareem Bawala | 04/04/2001 | Beginner |
This is a simple application that the gets the source of a webpage via the WebRequest Object.
The WebRequest class is defined in System.Net namespace. WebRequest is an abstract class. It is the base class for accessing data from the internet in the .Net Framework.
This tutorial would show you how to access data on the internet using the WebRequest Class.
Adding namespace Reference
Since the WebRequest class is defined in the System.Net namespace, so the first thing to do is to Add the System.Net reference to the project.
using System.Net;
Creating the WebRequest Object
The WebRequest Object should not be created directly. It is an abstract object. To create the WebRequest you should use the WebRequestFactory object. The WebRequestFactory class is a static class that returns an instance of an object derived from WebRequest.
You use the create method of the WebRequestFactory class. Here is an example:
// You could pass the create method a string or a URI
WebRequest WReq = WebRequestFactory.Create("http://www.yahoo.com");
OR
URI uriWebSite = new URI("http://www.yahoo.com");
WebRequest WReq = WebRequestFactory.Create(uriWebSite);
To get the response from the server you call the GetResponse() Method on the WebRequest object.
WebResponse wResp = WReq.GetResponse();
Getting the Stream of Data from the WebResponse Object
Getting the Stream of data you use the GetResponseStream() Method on the WebResponse Object and you specify how you want to encode the data you are getting back.
StreamReader sr = new StreamReader(WResp.GetResponseStream(), Encoding.ASCII);
That@#s it folks.
namespace HTMLSourceViewer { using System; using System.Net; using System.Text; using System.IO; using System.WinForms; public class GetWebPageSource { public GetWebPageSource() { } public GetWebPageSource(string webAddress) { this. webAddress = webAddress; } public string GetSource() { StringBuilder strSource = new StringBuilder(""); try { WebRequest WReq = WebRequestFactory.Create(this.webAddress); WebResponse WResp = WReq.GetResponse(); // get the stream of data StreamReader sr = new StreamReader(WResp.GetResponseStream(), Encoding.ASCII); string strTemp = ""; while ((strTemp = sr.ReadLine()) != null) { strSource.Append(strTemp + "\r\n"); } sr.Close(); } catch (WebException WebExcp) { MessageBox.Show(WebExcp.Message, "Error", MessageBox.IconError); } return strSource.ToString(); } // Accessor method public void setWebAddress(string strNewAddress) { this.webAddress = strNewAddress; } // private variables private string webAddress; } } namespace HTMLSourceViewer { using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.WinForms; using System.Net; using System.Net.Sockets; using System.Runtime.Remoting; public class Form1 : System.WinForms.Form { private System.ComponentModel.Container components; private System.WinForms.Label label2; private System.WinForms.TextBox textBox2; private System.WinForms.Button button2; private System.WinForms.Button button1; private System.WinForms.TextBox textBox1; private System.WinForms.Label label1; private const string HTTP = "http://"; // scheme identifier private string strSource = null; public Form1() { InitializeComponent(); this.Height = 100; } public override void Dispose() { base.Dispose(); components.Dispose(); } private void InitializeComponent() { this.components = new System.ComponentModel.Container (); this.button1 = new System.WinForms.Button (); this.label1 = new System.WinForms.Label (); this.label2 = new System.WinForms.Label (); this.textBox1 = new System.WinForms.TextBox (); this.button2 = new System.WinForms.Button (); this.textBox2 = new System.WinForms.TextBox (); //@this.TrayHeight = 0; //@this.TrayLargeIcon = false; //@this.DrawGrid = false; //@this.TrayAutoArrange = true; button1.Location = new System.Drawing.Point (544, 24); button1.ForeColor = System.Drawing.Color.Maroon; button1.BackColor = System.Drawing.Color.Khaki; button1.Size = new System.Drawing.Size (88, 24); button1.TabIndex = 2; button1.Text = "Get Source"; button1.Click += new System.EventHandler (this.button1_Click); label1.Location = new System.Drawing.Point (16, 24); label1.Text = "Web Address"; label1.Size = new System.Drawing.Size (89, 18); label1.BorderStyle = System.WinForms.BorderStyle.FixedSingle; label1.AutoSize = true; label1.Font = new System.Drawing.Font ("Microsoft Sans Serif", 10); label1.TabIndex = 0; label1.BackColor = System.Drawing.Color.Khaki; label2.Location = new System.Drawing.Point (16, 64); label2.Text = "WebPage Source"; label2.Size = new System.Drawing.Size (114, 18); label2.BorderStyle = System.WinForms.BorderStyle.FixedSingle; label2.AutoSize = true; label2.TabIndex = 5; label2.Anchor = System.WinForms.AnchorStyles.All; label2.BackColor = System.Drawing.Color.Khaki; label2.Visible = false; textBox1.Location = new System.Drawing.Point (112, 24); textBox1.Text = "http://"; textBox1.ForeColor = System.Drawing.Color.Maroon; textBox1.Font = new System.Drawing.Font ("Microsoft Sans Serif", 10); textBox1.TabIndex = 1; textBox1.Size = new System.Drawing.Size (424, 23); textBox1.BackColor = System.Drawing.Color.AntiqueWhite; button2.Location = new System.Drawing.Point (640, 24); button2.ForeColor = System.Drawing.Color.Maroon; button2.BackColor = System.Drawing.Color.Khaki; button2.Size = new System.Drawing.Size (56, 24); button2.TabIndex = 3; button2.Text = "Close"; button2.Click += new System.EventHandler (this.button2_Click); textBox2.Location = new System.Drawing.Point (16, 96); textBox2.Multiline = true; textBox2.BorderStyle = System.WinForms. BorderStyle.FixedSingle; textBox2.ScrollBars = System.WinForms.ScrollBars.Both; textBox2.ForeColor = System.Drawing.Color.Maroon; textBox2.TabIndex = 4; textBox2.Size = new System.Drawing.Size (680, 480); textBox2.BackColor = System.Drawing.Color.PapayaWhip; textBox2.Visible = false; this.Text = "Kareem@#s HTMLSource Viewer"; this.AutoScaleBaseSize = new System.Drawing.Size (6, 16); this.KeyPreview = true; this.AutoScroll = true; this.Font = new System.Drawing.Font ("Microsoft Sans Serif", 10); this.BackColor = System.Drawing.Color.Goldenrod; this.ClientSize = new System.Drawing.Size (704, 581); this.Controls.Add (this.label2); this.Controls.Add (this.textBox2); this.Controls.Add (this.button2); this.Controls.Add (this.button1); this.Controls.Add (this.textBox1); this.Controls.Add (this.label1); } protected void button2_Click (object sender, System.EventArgs e) { Application.Exit(); } protected override void OnKeyPress(KeyPressEventArgs e) { char chr = e.KeyChar; //13 = Enter key if (chr == 13) { GetPageSource(); } } protected void button1_Click (object sender, System.EventArgs e) { GetPageSource(); } public void GetPageSource() { string strAddress = textBox1.Text.Trim(); strAddress = strAddress.ToLower(); if ( strAddress.Length <= HTTP.Length) { MessageBox.Show("Enter a web address", "Web Address Field", MessageBox.IconInformation); textBox1.Text = HTTP; } else if (!strAddress.StartsWith(HTTP)) { MessageBox.Show("You have Entered the wrong Protocol.", "Wrong Scheme Identifier", MessageBox.IconInformation); textBox1.Text = HTTP; } else { // create the GetWebPageSource object GetWebPageSource objGS = HTMLSourceViewer.GetWebPageSource(); objGS.setWebAddress(strAddress); strSource = objGS.GetSource(); if (strSource.Length > 1) { showSource(); } } } public void showSource() { Form1.ActiveForm.Height = 608; textBox2.Text = strSource; label2.Visible = true; textBox2.Visible = true; } public static void Main(string[] args) { Form1 f1 = new Form1(); Application.Run(f1); } } } |
下一篇:asp.net中使用静态变量 >>
相关文章:
- · 网上下载和上传数据(一) Montaque(原作)
- · 一个SDK里做聊天室的例子(1)
- · 网上下载和上传数据(二) Montaque(原作)
- · 有空的时候看看,:)ASP.NET Page Templates
- · VB.NET开发互联网应用
- · vb.net cookie操作
- · Net中如何操作IIS(原理篇)
- · 关于选用何种ASP.NET设计方法的技巧
- · .Net中如何操作IIS(源代码) (原创)
- · iis 坏掉了,重新安装了以后.netframework 不能用了的解决方法
- · 两个aspx页面间传递引用对象。
- · 在Webcontrol的Toolbar上加入删除确认的方法(改进后)
- · TreeView 派生类: TreeViewEx 实现 NodeShowToolTip、NodeDoubleClick 事件
- · 我自己写的自定义Web的上传控件
- · 增加判断文字长度,汉字算2个
- · 客户端脚本对中文的验证(javascript)
- · 献丑了,我的asp.net网站开发经验,欢迎参加讨论。
- · 笑望人生,关于IHttpHandler处理图片
- · HTML在线编辑器--服务器控件~~.NET实现~~
- · How to Share Session State Between Classic ASP and ASP.NET(1)
- · How to Share Session State Between Classic ASP and ASP.NET(2)
- · 关于验证控件,希望对和我原来有疑惑的朋友有帮助(刚找的资料,结合猫猫的)
- · 上次的一个问题我打了微软的求助电话,他们也没有办法!
- · [技巧]DataGird的hyper column的url field 绑定两个字段
- · ms--help
- · 续
- · Simple Paging in Repeater and DataList Controls
- · ASP.NET编程中的十大技巧(建议进精华)
- · 转贴:DataGrid/DataList
- · 用ASP.NET写你自己的代码生成器(1)。
- · ASP.NET中Cookie编程的基础知识(6)
- · ASP.NET中Cookie编程的基础知识(5)
- · ASP.NET中Cookie编程的基础知识(4)
- · ASP.NET中Cookie编程的基础知识(3)
- · ASP.NET中Cookie编程的基础知识(2)
- · ASP.NET中Cookie编程的基础知识(1)
- · .NET中窗体间相互访问的几种方式
- · .net中PictureBox中图片的拖动
