Team downloader in the .NET environment (2)

xiaoxiao2021-03-06  38

Web page download is under construction .NET environment (2) Author: Wang Kaiming www.ASPCool.com Time: 2003-8-29 13:21:38 Views: 5358

three. Web downloader instance introduction: Finally, I will show some of the best examples of the above .NET network programming. This example is a client application that uses Socket based synchronous mode, which first establishes an endpoint by parsing the IP address of the server, and creates a streamlined socket connection, the protocol used is the TCP protocol. With this Socket, you can send a command to obtain a web page, and then obtain the default web page on the server through the Socket, and finally the data obtained by the file stream will be written to the local file. This completes the download work of the web page. The effect of the program run is as follows: The code code is as follows: use system; use system.drawing; using system.collections; using system.componentmodel; using system.windows.form; .Data; using system.net.sockets; using system.text; usingspace socketsample {////

/// Form1 summary description. /// 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; /// // / / must-have designer variable. /// private system.componentmodel.container component = null; public form1 () {// // Windows Form Designer Support for // InitializeComponent (); // // Todo: in InitializeComponent / / After calling any constructor code} /// /// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = null) {Components.dispose ();}} Base.Dispose (4);} #Region Windows Form Designer Generated code /// /// Designer supports the required method - do not use the code editor to modify the // this method.

/// private () {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 = "server address:"; 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 = "local File name: "; this.label2.textalign = system.drawing. ContentAlignment.middl Eright; // // Download // this.download.location = new system. Drawing.point (288, 24); this.download.name = "Download"; this.download.TabINDEX = 2; this.download.Text = "Start download"; 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 = "web downloader"; this.ResumeLayout (false);} #ENDREGION ///

// Application main entry.

/// [stathread] static void main () {Application.run (new form1 ());} private string dosocketget (String Server) {// Defines some necessary variables and characters to send to the server String encoding ascii = encoding.ascii; string get = "Get / http / 1.1 / r / nhost:" Server "/ r / nConnection: close / r / n / r / n"; byte [] byteget = ascii.getibytes (GET); byte [] recvbytes = new byte [256]; String strretpage = null; // Gets the server-related IP address list, where the first item is the ipaddress hostd = DNS.Resolve (Server) of us. AddressList [0]; // Create a endpoint based on the IP address of the obtained server, the port is the default 80 IpendPoint Ephost = New IpendPoint (HostAdd, 80); // Create a Socket Instance Socket S = New Socket (AddressFamily. Internetwork , SocketType.stream, protocoltype.tcp; try {// connected to the server S.Connect (Ephost) by the endpoint acquired;} catch (exception se) {messagebox.show ("Connection error:" se. Message, "Tips", MessageBoxButtons. Retrycancel, MessageBoxicon.information;}} (! S.connected) {strretpage = "Cannot be connected to the server!"; Return Strre TPAGE;} Try {// Send GET commands to the server S.send (Byteget, Byteget.Length, SocketFlags.none);} catch (Exception CE) {MessageBox.show ("Send Error:" CE. Message, "Tips Information ", MessageBoxButtons. [,",} // receive page data until all bytes are received int32 bytes = S.Receive (RecvBytes, Recvbytes.length, 0); strretpage = "The following is server" Server "Default web page: / r / n"; strretpage = strretpage ascii.getstring (recvbytes, 0, bytes); while (bytes> 0) {bytes =

转载请注明原文地址:https://www.9cbs.com/read-72601.html

New Post(0)