Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->编程语言 ->ASP.NET ->正文

构造.NET环境下的网页下载器 (2)

来源:Linuxdby.com 作者:Webmaster 时间:2007-04-28 点击: [收藏] [投稿]
三.网页下载器实例介绍:

  最后,我就综合以上.NET网络编程的一些知识,向大家展示一个很好的实例。该实例是一个运用Socket的基于同步模式的客户端应用程序,它首先通过解析服务器的IP地址建立一个终结点,同时创建一个基于流套接字的Socket连接,其运用的协议是TCP协议。通过该Socket就可以发送获取网页的命令,再通过该Socket获得服务器上默认的网页,最后通过文件流将获得的数据写入本机文件。这样就完成了网页的下载工作了

程序的代码如下:

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.Text;
using System.IO;

namespace SocketSample
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button Download;


private System.Windows.Forms.TextBox ServerAddress;
private System.Windows.Forms.TextBox Filename;
/// <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.label2 = new System.Windows.
Forms.Label();
this.Download = new System.Windows.
Forms.Button();
this.ServerAddress = new System.Windows.
Forms.TextBox();
this.Filename = new System.Windows.
Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.
Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.
Size(80, 23);
this.label1.TabIndex = 0;
this.label1.Text = "服务器地址:";
this.label1.TextAlign = System.Drawing.
ContentAlignment.MiddleRight;
//
// label2
//
this.label2.Location = new System.Drawing.
Point(16, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.
Size(80, 23);
this.label2.TabIndex = 1;
this.label2.Text = "本地文件名:";
this.label2.TextAlign = System.Drawing.
ContentAlignment.MiddleRight;
//
// Download
//
this.Download.Location = new System.
Drawing.Point(288, 24);
this.Download.Name = "Download";

this.Download.TabIndex = 2;
this.Download.Text = "开始下载";
this.Download.Click += new System.
EventHandler(this.Download_Click);
//
// ServerAddress
//
this.ServerAddress.Location = new System.
Drawing.Point(96, 24);
this.ServerAddress.Name = "ServerAddress";
this.ServerAddress.Size = new System.
Drawing.Size(176, 21);
this.ServerAddress.TabIndex = 3;
this.ServerAddress.Text = "";
//
// Filename
//
this.Filename.Location = new System.
Drawing.Point(96, 64);
this.Filename.Name = "Filename";
this.Filename.Size = new System.
Drawing.Size(176, 21);
this.Filename.TabIndex = 4;
this.Filename.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.
Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.
Size(376, 117);
this.Controls.AddRange(new System.Windows.
Forms.Control[] {
this.Filename,
this.ServerAddress,
this.Download,
this.label2,
this.label1});
this.Name = "Form1";
this.Text = "网页下载器";
this.ResumeLayout(false);
}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private string DoSocketGet(string server)
{
//定义一些必要的变量以及一条要发送到服务器的字符串
Encoding ASCII = Encoding.ASCII;
string Get = "GET / HTTP/1.1\r\nHost: "
+server+"\r\nConnection: Close\r\n\r\n";
Byte[] ByteGet = ASCII.GetBytes(Get);
Byte[] RecvBytes = new Byte[256];
String strRetPage = null;

//获取服务器相关的IP地址列表,其中第一项即为我们所需的
IPAddress hostadd = Dns.Resolve(server).
AddressList[0];

//根据获得的服务器的IP地址创建一个终结点,端口为默认的80
IPEndPoint EPhost = new IPEndPoint
(hostadd, 80);

//创建一个Socket实例

 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:杂志目录(页面部分)   下一篇:推荐一个免费的CSharp编辑器

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号