From 14f97ce425e59ccc9cddc4bac1325d14823f3841 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Sun, 26 May 2024 23:45:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Docs/1.2.使用准则.ipynb | 105 +++++++++++++++++- Docs/1.5.总结.ipynb | 6 + .../Utilities/StartupUtility.cs | 2 +- HttpClientStudy.UnitTest/startup.cs | 2 +- HttpClientStudy.WebClient/Program.cs | 2 +- 5 files changed, 112 insertions(+), 5 deletions(-) diff --git a/Docs/1.2.使用准则.ipynb b/Docs/1.2.使用准则.ipynb index 6d28117..e34edc5 100644 --- a/Docs/1.2.使用准则.ipynb +++ b/Docs/1.2.使用准则.ipynb @@ -215,14 +215,14 @@ " await Task.Delay(TimeSpan.FromSeconds(2));\n", "}\n", "\n", - "Console.WriteLine(\"请在程序退出后,执行下面命令行查看网络情况\");\n" + "Console.WriteLine(\"程序运行大约要10-20秒,请在程序退出后,执行下面命令行查看网络情况\");\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "使用我们刚刚讨论的设置,此代码依次向同一端点发出5个请求。在每个请求之间,它会暂停两秒钟。该代码还输出从DNS检索到的Google服务器的IPv4地址。我们可以使用此IP地址来查看通过PowerShell中发出的netstat命令对其打开的连接:" + "使用自定义设置,依次向同一端点发出5个请求。在每个请求之间,暂停两秒钟。输出从DNS检索到的网站服务器的IPv4地址。我们可以使用此IP地址来查看通过PowerShell中发出的netstat命令对其打开的连接:" ] }, { @@ -248,6 +248,107 @@ "netstat -ano | findstr $queryIp" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "在这种情况下,到远程端点的连接只有1个。在每个请求之后,该连接将返回到池中,因此在发出下一个请求时可以重新使用。\n", + "如果更改连接的生存期,以使它们在1秒后过期,测试这对行为的影响:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "using System.IO;\n", + "using System.Diagnostics;\n", + "using System.Net;\n", + "using System.Net.Http;\n", + "\n", + "var ips = await Dns.GetHostAddressesAsync(\"soft.pwidc.cn\");\n", + "string firstIp = ips.FirstOrDefault().ToString();\n", + "\t\n", + "foreach (var ipAddress in ips)\n", + "{\n", + " Console.WriteLine(ipAddress.MapToIPv4().ToString());\n", + "}\n", + "\n", + "//自定义行为\n", + "var socketsHandler2 = new SocketsHttpHandler\n", + "{\n", + " PooledConnectionLifetime = TimeSpan.FromSeconds(1),\n", + " PooledConnectionIdleTimeout = TimeSpan.FromSeconds(1),\n", + " MaxConnectionsPerServer = 1\n", + "};\n", + "\n", + "var client2 = new HttpClient(socketsHandler2);\n", + "\n", + "for (var i = 0; i < 3; i++)\n", + "{\n", + " if(i>0)\n", + " {\n", + " await Task.Delay(TimeSpan.FromSeconds(2));\n", + " }\n", + " _ = await client2.GetAsync(\"http://soft.pwidc.cn\");\n", + "}\n", + "\n", + "Console.WriteLine(\"程序运行大约要10-20,请在程序退出后,执行下面命令行查看网络情况\");\n", + "\n", + "//调用命令行,显示查看网络情况\n", + "string command = $\"netstat -ano | findstr {firstIp}\";\n", + " \n", + "// 创建一个新的ProcessStartInfo对象\n", + "ProcessStartInfo startInfo = new ProcessStartInfo(\"cmd\", $\"/c {command}\")\n", + "{\n", + " RedirectStandardOutput = true, // 重定向标准输出\n", + " UseShellExecute = false, // 不使用系统外壳程序启动\n", + " CreateNoWindow = true // 不创建新窗口\n", + "};\n", + " \n", + "// 启动进程\n", + "using (Process process = Process.Start(startInfo))\n", + "{\n", + " // 读取cmd的输出\n", + " using (StreamReader reader = process.StandardOutput)\n", + " {\n", + " string stdoutLine = reader.ReadToEnd();\n", + " Console.WriteLine(stdoutLine);\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "dotnet_interactive": { + "language": "pwsh" + }, + "polyglot_notebook": { + "kernelName": "pwsh" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "#!set --value @csharp:firstIp --name queryIp\n", + "netstat -ano | findstr $queryIp" + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/Docs/1.5.总结.ipynb b/Docs/1.5.总结.ipynb index 92d6ad9..0d0b0d0 100644 --- a/Docs/1.5.总结.ipynb +++ b/Docs/1.5.总结.ipynb @@ -35,6 +35,12 @@ "cell_type": "code", "execution_count": null, "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, "vscode": { "languageId": "polyglot-notebook" } diff --git a/HttpClientStudy.Core/Utilities/StartupUtility.cs b/HttpClientStudy.Core/Utilities/StartupUtility.cs index 7f59c29..c6ccee5 100644 --- a/HttpClientStudy.Core/Utilities/StartupUtility.cs +++ b/HttpClientStudy.Core/Utilities/StartupUtility.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace HttpClientStudy.Core.Utils +namespace HttpClientStudy.Core.Utilities { /// /// 启动工具类 diff --git a/HttpClientStudy.UnitTest/startup.cs b/HttpClientStudy.UnitTest/startup.cs index 3604942..ca9268e 100644 --- a/HttpClientStudy.UnitTest/startup.cs +++ b/HttpClientStudy.UnitTest/startup.cs @@ -1,6 +1,6 @@ using System.Diagnostics; -using HttpClientStudy.Core.Utils; +using HttpClientStudy.Core.Utilities; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; diff --git a/HttpClientStudy.WebClient/Program.cs b/HttpClientStudy.WebClient/Program.cs index cc81b07..02fa311 100644 --- a/HttpClientStudy.WebClient/Program.cs +++ b/HttpClientStudy.WebClient/Program.cs @@ -1,6 +1,6 @@ using System.Diagnostics; -using HttpClientStudy.Core.Utils; +using HttpClientStudy.Core.Utilities; //WebApi StartupUtility.StartWebApiProject();