C#浏览网页源代码

来源:互联网 发布:网络销售最好的是什么 编辑:程序博客网 时间:2024/06/05 17:50
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
 
namespace WebApplication2
{
public class GetWebPageSource
{
public GetWebPageSource()
{}
public GetWebPageSource(string webAddress)
{
this.webAddress=webAddress;
}
public string GetSource()
{
StringBuilder strSource=new StringBuilder("");
try
{
WebRequest WReq=WebRequest.Create(this.webAddress);
WebResponse WResp=WReq.GetResponse();
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",MessageBoxButtons.OK);
}
return strSource.ToString();
}
public void setWebAddress(string strNewAddress)
{
this.webAddress= strNewAddress;
}
private string webAddress;
}
 
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
 
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
 
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
 
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
}
 
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// label1
// 
this.label1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(148, 24);
this.label1.TabIndex = 0;
this.label1.Text = "输入URL,并回车:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
// 
// textBox1
// 
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right);
this.textBox1.Location = new System.Drawing.Point(128, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
// 
// button2
// 
this.button2.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.button2.Location = new System.Drawing.Point(324, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(48, 24);
this.button2.TabIndex = 4;
this.button2.Text = "EXIT";
this.button2.Click += new System.EventHandler(this.exitButton_Click);
// 
// textBox2
// 
this.textBox2.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
| System.Windows.Forms.AnchorStyles.Left) 
| System.Windows.Forms.AnchorStyles.Right);
this.textBox2.Location = new System.Drawing.Point(0, 32);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox2.Size = new System.Drawing.Size(372, 317);
this.textBox2.TabIndex = 5;
this.textBox2.Text = "textBox2";
// 
// button1
// 
this.button1.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.button1.Location = new System.Drawing.Point(272, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 24);
this.button1.TabIndex = 6;
this.button1.Text = "CODE";
this.button1.Click += new System.EventHandler(this.codeBtn_click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(376, 350);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
 this.button1,
 this.textBox2,
 this.button2,
 this.textBox1,
 this.label1});
this.Name = "Form1";
this.Text = "WebPageSource";
this.ResumeLayout(false);
 
}
#endregion
 
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}
 
private void exitButton_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
 
/*private void URLtextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
char chr=e.KeyChar;
if (chr==13)
{
GetPageSource();
}
}*/
public void GetPageSource()
{
string strAddress=textBox1.Text.Trim();
strAddress=strAddress.ToLower();
GetWebPageSource objGS= new GetWebPageSource();
objGS.setWebAddress(strAddress);
strS=objGS.GetSource();
if(strS.Length>1)
{
showSource();
}
}
private void showSource()
{
Form1.ActiveForm.Height=608;
textBox2.Text=strS;
textBox2.Visible=true;
}
 
public string strS;
 
private void codeBtn_click(object sender, System.EventArgs e)
{
GetPageSource();
}
}

}


原创粉丝点击