扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
源代码如下:(其中主要的函数为DoSocketGet())
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
//
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
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private string DoSocketGet(string server)
{
file://定义一些必要的变量以及一条要发送到服务器的字符串
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;
file://获取服务器相关的IP地址列表,其中第一项即为我们所需的
IPAddress hostadd = Dns.Resolve(server).AddressList[0];
file://根据获得的服务器的IP地址创建一个终结点,端口为默认的80
IPEndPoint EPhost = new IPEndPoint(hostadd, 80);
file://创建一个Socket实例
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );
try
{
file://用上面所取得的终结点连接到服务器
s.Connect(EPhost);
}
catch(Exception se)
{
MessageBox.Show("连接错误:"+se.Message,"提示信息",
MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);
}
if (!s.Connected)
{
strRetPage = "不能连接到服务器!";
return strRetPage;
}
try
{
file://向服务器发送GET命令
s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
}
catch(Exception ce)
{
MessageBox.Show("发送错误:"+ce.Message,"提示信息",
MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);
}
file://接收页面数据,直到所有字节接收完毕
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = "以下是在服务器" + server + "上的默认网页:\r\n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, SocketFlags.None);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}
file://禁用并关闭Socket实例
s.Shutdown(SocketShutdown.Both);
s.Close();
return strRetPage;
}
private void Download_Click(object sender, System.EventArgs e)
{
file://将所读取的字符串转换为字节数组
byte[] content=Encoding.ASCII.GetBytes(DoSocketGet(ServerAddress.Text));
try
{
file://创建文件流对象实例
FileStream fs=new FileStream(Filename.Text,FileMode.OpenOrCreate,FileAccess.ReadWrite);
file://写入文件
fs.Write(content,0,content.Length);
}
catch(Exception fe)
{
MessageBox.Show("文件创建/写入错误:"+fe.Message,"提示信息",MessageBoxButtons.RetryCancel,MessageBoxIcon.Information);
}
}
}
}
以上程序在Windows 2000服务器版、Visual Studio.Net中文正式版下调试通过
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者