⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 里面包括很多网络的模拟 有一个网页下载工具 基于控制台
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;


namespace HttpGet
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs = new FileStream("aTest.htm", FileMode.Create);

            try
            {
                TcpClient newclient = new TcpClient("www.hao123.com", 80);
                NetworkStream ns = newclient.GetStream();
                byte[] outbytes = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nHost:www.hao123.com\r\nUser-agent:Mozilla/4.0\r\nConnection:close\r\n\r\n");
                ns.Write(outbytes, 0, outbytes.Length);
                byte[] inbytes = new byte[1024];
                int readBytes, totalBytes = 0 ;

                readBytes = ns.Read(inbytes, 0, inbytes.Length);
                while ( readBytes !=0 )
                {

                    totalBytes += readBytes;
                    fs.Write(inbytes, 0, readBytes);

                    
                    readBytes = ns.Read(inbytes, 0, inbytes.Length);
                    Console.WriteLine("readBytes {0}, totalBytes {1}", readBytes, totalBytes);

                }
                
                fs.Close();
                ns.Close();
                newclient.Close();
                //Console.WriteLine("The End");
                
                
            }
            catch (ObjectDisposedException e)
            {
                Console.WriteLine("IOException caught!!!");
                Console.WriteLine("Source : " + e.Source);
                Console.WriteLine("Message : " + e.Message);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException caught!!!");
                Console.WriteLine("Source : " + e.Source);
                Console.WriteLine("Message : " + e.Message);
            }
            catch (IOException e)
            {
                 Console.WriteLine("IOException caught!!!");
                 Console.WriteLine("Source : " + e.Source);
                 Console.WriteLine("Message : " + e.Message);
            }


            Console.ReadKey();

        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -