Web page | Download three. Web Page Downloader Example Introduction:
Finally, I will synthesize the above. NET programming knowledge, to show you a good example. This example is a synchronous-mode client application using a socket, which first establishes an endpoint by parsing the IP address of the server, and creates a socket connection based on a stream socket, which is used by the TCP protocol. Through the socket can be sent to get the page command, and then through the socket to get the default Web page, and finally through the file stream will be obtained data to the local file. This completes the download work of the Web page
The code for the program is as follows:
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>
Summary description of the 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>
The required designer variable.
</summary>
Private System.ComponentModel.Container
components = NULL;
Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
//
TODO: In InitializeComponent
Add any constructor code after calling
}
<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
#region Windows Form Designer generated code
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</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 = "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 filename:";
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 = "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
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}
private string Dosocketget (String server)
{
Define the necessary variables and a string to send to the 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;
Get a list of server-related IP addresses, where the first item is what we need
IPAddress Hostadd = dns.resolve (server).
ADDRESSLIST[0];
Creates an endpoint based on the IP address of the server being obtained, with the port default of 80
IPEndPoint ephost = new IPEndPoint
(Hostadd, 80);
Create a Socket instance
Socket s = new socket (addressfamily.
Internetwork, SocketType.Stream,
PROTOCOLTYPE.TCP);
Try
{
Connect to the server with the endpoint obtained above
S.connect (Ephost);
}
catch (Exception se)
{
MessageBox.Show ("Connection error:" +se.)
message, "Hint info", MessageBoxButtons.
Retrycancel,messageboxicon.information);
}
if (!s.connected)
{
Strretpage = "Cannot connect to the server!" ";
return strretpage;
}
Try
{
Send a GET command to the server
S.send (Byteget, Byteget.length,
Socketflags.none);
}
catch (Exception CE)
{
MessageBox.Show ("Send error:" +ce.)
message, "Hint info", MessageBoxButtons.
Retrycancel,messageboxicon.information);
}
Receive page data until all bytes are received
Int32 bytes = s.receive (Recvbytes,
Recvbytes.length, 0);
Strretpage = "The following is on server" + server +
"Default page on: \ r \ n";
Strretpage = Strretpage + ASCII. GetString
(recvbytes, 0, bytes);
The main function is Dosocketget (), first the program in response to the "Start Download" event handler Download_click (), call the Dosocketget () function, the function completes the socket creation, connection, and communication with the host-that is, to get the Web page on the host, Disable, turn off, and so on. After the Dosocketget () function is called, the Download_click () function creates a FileStream object and attempts to write the Web page file returned by the Dosocketget () function to a native file as a byte array. The final form of an HTML file on this computer completes the download of a Web page file.
However, the function of this program is relatively simple, can not be used as a real web browser, the Web page file download or to use IE and other browsers to open. As an explanation however. NET low-level network programming example is definitely a good example, so I hope the reader can study well, while the reader can also add File download progress bar to improve this program.
Note: The above program is debugged in Windows 2000 Server Edition, Visual Studio.NET Chinese official edition.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.