当前位置: 首页 > news >正文

政府网站建设及管理规范 云南大连企业网站建站模板

政府网站建设及管理规范 云南,大连企业网站建站模板,西安网站制作推广,网站备案一般多久网络编程是现代应用程序开发中至关重要的一部分。C# 提供了一套丰富的 API 来处理基本网络通信、Web请求与响应。在本节中,我们将深入探讨这些内容,帮助您掌握如何在 C# 中进行网络编程。 基本网络通信 基本网络通信通常涉及套接字(Socket&a…

        网络编程是现代应用程序开发中至关重要的一部分。C# 提供了一套丰富的 API 来处理基本网络通信、Web请求与响应。在本节中,我们将深入探讨这些内容,帮助您掌握如何在 C# 中进行网络编程。

基本网络通信

        基本网络通信通常涉及套接字(Socket)编程。C# 提供了 ‘System.Net.Sockets‘ 命名空间来处理套接字编程,使得开发者可以创建客户端和服务器应用程序。

        以下是使用 TCP 套接字创建一个简单的客户端-服务器应用程序的方法示例:

服务器端

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;public class Server
{public static void Main(){TcpListener server = null;try{int port = 13000;IPAddress localAddr = IPAddress.Parse("127.0.0.1");server = new TcpListener(localAddr, port);server.Start();byte[] bytes = new byte[256];string data = null;while (true){Console.Write("Waiting for a connection... ");TcpClient client = server.AcceptTcpClient();Console.WriteLine("Connected!");data = null;NetworkStream stream = client.GetStream();int i;while ((i = stream.Read(bytes, 0, bytes.Length)) != 0){data = Encoding.ASCII.GetString(bytes, 0, i);Console.WriteLine($"Received: {data}");data = data.ToUpper();byte[] msg = Encoding.ASCII.GetBytes(data);stream.Write(msg, 0, msg.Length);Console.WriteLine($"Sent: {data}");}client.Close();}}catch (SocketException e){Console.WriteLine($"SocketException: {e}");}finally{server.Stop();}Console.WriteLine("\nHit enter to continue...");Console.Read();}
}

客户端

using System;
using System.Net.Sockets;
using System.Text;public class Client
{public static void Main(){try{Int32 port = 13000;TcpClient client = new TcpClient("127.0.0.1", port);NetworkStream stream = client.GetStream();string message = "Hello, Server!";byte[] data = Encoding.ASCII.GetBytes(message);stream.Write(data, 0, data.Length);Console.WriteLine($"Sent: {message}");data = new byte[256];string responseData = string.Empty;Int32 bytes = stream.Read(data, 0, data.Length);responseData = Encoding.ASCII.GetString(data, 0, bytes);Console.WriteLine($"Received: {responseData}");stream.Close();client.Close();}catch (ArgumentNullException e){Console.WriteLine($"ArgumentNullException: {e}");}catch (SocketException e){Console.WriteLine($"SocketException: {e}");}Console.WriteLine("\nHit enter to continue...");Console.Read();}
}

        在这个示例中,服务器端使用 ‘TcpListener‘ 类监听连接请求,客户端使用 ‘TcpClient‘ 类连接到服务器并发送数据。

Web请求与响应

        Web请求与响应是网络编程中的常见操作。C# 提供了 ‘System.Net.Http‘ 命名空间,用于处理 HTTP 请求和响应。

发送 GET 请求

using System;
using System.Net.Http;
using System.Threading.Tasks;public class Program
{private static readonly HttpClient client = new HttpClient();public static async Task Main(){try{HttpResponseMessage response = await client.GetAsync("https://api.github.com/repos/dotnet/docs/issues");response.EnsureSuccessStatusCode();string responseBody = await response.Content.ReadAsStringAsync();Console.WriteLine(responseBody);}catch (HttpRequestException e){Console.WriteLine($"Request exception: {e.Message}");}}
}

        在这个示例中,使用 ‘HttpClient‘ 类发送 GET 请求,并读取响应内容。

发送 POST 请求

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;public class Program
{private static readonly HttpClient client = new HttpClient();public static async Task Main(){var values = new Dictionary<string, string>{{ "username", "example" },{ "password", "password" }};var content = new FormUrlEncodedContent(values);try{HttpResponseMessage response = await client.PostAsync("https://example.com/api/login", content);response.EnsureSuccessStatusCode();string responseBody = await response.Content.ReadAsStringAsync();Console.WriteLine(responseBody);}catch (HttpRequestException e){Console.WriteLine($"Request exception: {e.Message}");}}
}

        在这个示例中,使用 ‘HttpClient‘ 类发送 POST 请求,并读取响应内容。

总结

        在本节中,我们探讨了 C# 中进行网络编程的多种方法,包括基本网络通信和 Web 请求与响应。通过掌握这些技术,您可以开发网络应用程序,实现客户端和服务器之间的通信,处理 HTTP 请求和响应,提升应用程序的功能和性能。继续学习 C# 的高级特性,将使您成为更为高效和熟练的开发者。

http://www.khdw.cn/news/47272.html

相关文章:

  • 政府网站页面布局评价网文推广怎么做
  • 公司网站费用怎么做会计分录品牌推广策划方案怎么写
  • 网站标题正确书写标准深圳网站seo服务
  • 做统计的网站百度站长之家工具
  • wordpress占用大seo优化服务价格
  • 北京北京网站建设网站主题
  • 如何修改公司网站内容山西seo排名
  • 免费阅读小说大全下载手机端关键词排名优化
  • Myeclipse怎么做网站湖南网站建设推广优化
  • 帮人家做网站怎么赚钱网站优化排名方案
  • 网站开发设计运维google站长工具
  • 小网站郑州网络seo公司
  • 北京市建设部网站海外推广渠道都有哪些
  • wordpress媒体库创建文件夹网站关键词优化多少钱
  • 网站排名需要多长时间刷网站排名软件
  • 10个网站沈阳关键词seo
  • 厦门seo排名公司抖音seo搜索引擎优化
  • 广东做网站微平台推广
  • 黄色为主的网站网上推广方式
  • wap网站开发教程珠海网站建设优化
  • 网站建设怎样把网页连接起来成功营销十大经典案例
  • 南通启益建设集团有限公司网站青岛网站开发公司
  • 成都网站建设开发公司哪家好seo站长工具平台
  • 百度怎么验证网站做网站优化的公司
  • 厦门协会网站建设手机上如何制作自己的网站
  • 品牌好的佛山网站建设价格百度贴吧怎么发广告
  • 新疆工程建设云网站百度百科ui设计
  • 51网站一起做网店360推广
  • 住房和建设局网站seo综合
  • 快速网站排名提升工具网络营销的定义是什么