main
bicijinlian 11 months ago
parent 995a4a0648
commit 14f97ce425

@ -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": {},

@ -35,6 +35,12 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
},
"vscode": {
"languageId": "polyglot-notebook"
}

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HttpClientStudy.Core.Utils
namespace HttpClientStudy.Core.Utilities
{
/// <summary>
/// 启动工具类

@ -1,6 +1,6 @@
using System.Diagnostics;
using HttpClientStudy.Core.Utils;
using HttpClientStudy.Core.Utilities;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;

@ -1,6 +1,6 @@
using System.Diagnostics;
using HttpClientStudy.Core.Utils;
using HttpClientStudy.Core.Utilities;
//启动WebApi程序
StartupUtility.StartWebApiProject();

Loading…
Cancel
Save