Skip to Content
C# 12 核心要点
book

C# 12 核心要点

by Joseph Albahari
May 2025
Intermediate to advanced
1086 pages
14h 54m
Chinese
O'Reilly Media, Inc.
Content preview from C# 12 核心要点

第 16 章 网络 Network+

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

.NET 在System.Net.* 命名空间中提供了各种类,用于通过标准网络协议(如 HTTP 和 TCP/IP)进行通信。下面是主要组件的摘要:

  • HttpClient 用于消费 HTTP 网络 API 和 RESTful 服务

  • HttpListener 用于编写 HTTP 服务器

  • SmtpClient 用于通过 SMTP 构建和发送邮件信息

  • Dns 用于域名和地址之间的转换

  • TcpClient,UdpClient,TcpListener, 和Socket 类,用于直接访问传输层和网络层

本章中的 .NET 类型属于System.Net.*System.IO 命名空间。

备注

.NET也为FTP提供客户端支持,但只能通过从.NET 6开始就被标记为过时的类来实现。如果您需要使用FTP,最好的选择是使用NuGet库,如FluentFTP。

Network+ 架构

图 16-1展示了 .NET 网络类型及其所在的通信层。大多数类型位于传输层应用层。传输层定义了发送和接收字节的基本协议(TCP 和 UDP);应用层定义了为特定应用设计的高级协议,如检索网页(HTTP)、发送邮件(SMTP)以及域名和 IP 地址之间的转换(DNS)。

图 16-1. 网络架构

通常,在应用层编程是最方便的;不过,也有几个原因让您想直接在传输层工作。一个原因是您需要一个.NET中没有提供的应用程序协议,如用于检索邮件的 POP3。另一种情况是,您想为特殊应用程序(如点对点客户端)发明一种自定义协议。

在应用协议中,HTTP 的特殊之处在于它适用于通用通信。它的基本操作模式--"用这个URL给我网页"--可以很好地转换为 "用这些参数给我调用这个端点的结果"。(除了 "get "动词外,还有 "put"、"post "和 "delete "等动词,可以提供基于 REST 的服务)。

HTTP 还具有在多层业务应用程序和面向服务架构中非常有用的丰富功能,如身份验证和加密协议、消息分块、可扩展报头和 Cookie,以及让许多服务器应用程序共享一个端口和 IP 地址的能力。基于这些原因,.NET 对 HTTP 提供了很好的支持,包括本章所述的直接支持,以及通过 Web API 和 ASP.NET Core 等技术提供的更高级别的支持。

正如前面的讨论所表明的,Network 是一个充斥着大量首字母缩略词的领域。表 16-1 列出了最常见的缩略语。

表 16-1. Network+ 缩写词
缩略语 扩建 说明
DNS 域名服务 在域名(如ebay.com)和 IP 地址(如 199.54.213.2)之间进行转换
文件传输协议 文件传输协议 基于互联网的文件收发协议
超文本传输协定 超文本传输协议 检索网页和运行网络服务
IIS 互联网信息服务 微软的网络服务器软件
IP 互联网协议 低于 TCP 和 UDP 的网络层协议
局域网 局域网 大多数局域网使用基于互联网的协议,如 TCP/IP
持久性有机污染物 邮局礼宾 检索互联网邮件
REST 呈现状态转移 一种流行的网络服务架构,在响应中使用机器可跟踪链接,可通过基本 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming C# 12

Programming C# 12

Ian Griffiths
C# 12 in a Nutshell

C# 12 in a Nutshell

Joseph Albahari
C# 6 for Programmers, Sixth Edition

C# 6 for Programmers, Sixth Edition

Paul Deitel, Harvey Deitel
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 9798341657038