構造.NET環境下的頁面下載器 (2)
發表時間:2024-02-10 來源:明輝站整理相關軟件相關文章人氣:
[摘要]三.網頁下載器實例介紹: 最后,我就綜合以上.NET網絡編程的一些知識,向大家展示一個很好的實例。該實例是一個運用Socket的基于同步模式的客戶端應用程序,它首先通過解析服務器的IP地址建立一個終結點,同時創建一個基于流套接字的Socket連接,其運用的協議是TCP協議。通過該...
三.網頁下載器實例介紹:
最后,我就綜合以上.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
{
///
/// Form1 的摘要說明。
/// 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;
///
/// 必需的設計器變量。
/// private System.ComponentModel.Container
components = null;
public Form1()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent
// 調用后添加任何構造函數代碼
}
///
/// 清理所有正在使用的資源。
/// protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// 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
[1] [2] 下一頁