diff --git a/Docs/1.3.1.基础使用.发送请求.ipynb b/Docs/1.3.1.基础使用.发送请求.ipynb index 7a311aa..188f335 100644 --- a/Docs/1.3.1.基础使用.发送请求.ipynb +++ b/Docs/1.3.1.基础使用.发送请求.ipynb @@ -33,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" @@ -45,32 +45,14 @@ "languageId": "polyglot-notebook" } }, - "outputs": [ - { - "data": { - "text/html": [ - "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Microsoft.Extensions.DependencyInjection, 8.0.0</span></li><li><span>Microsoft.Extensions.Http, 8.0.0</span></li><li><span>Microsoft.Net.Http.Headers, 8.0.8</span></li><li><span>System.Net.Http.Json, 8.0.0</span></li></ul></div></div>" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "配置文件根目录:e:\\王高峰\\我的项目\\学习项目\\HttpClientStudy\\Docs\\Publish\\HttpClientStudy.Core\n", - "启动WebApi项目\n", - "程序[e:\\王高峰\\我的项目\\学习项目\\HttpClientStudy\\Docs\\Publish\\HttpClientStudy.WebApp\\HttpClientStudy.WebApp.exe]已在新的命令行窗口执行。如果未出现新命令行窗口,可能是程序错误造成窗口闪现!\n" - ] - } - ], + "outputs": [], "source": [ "//全局设置,行运行一次,为后续准备\n", "#r \"nuget:System.Net.Http.Json\"\n", "#r \"nuget:Microsoft.Net.Http.Headers\"\n", "#r \"nuget:Microsoft.Extensions.Http\"\n", "#r \"nuget:Microsoft.Extensions.DependencyInjection\"\n", + "#r \"./Publish/HttpClientStudy.Model/HttpClientStudy.Model.dll\"\n", "#r \"./Publish/HttpClientStudy.Core/HttpClientStudy.Core.dll\"\n", "\n", "global using System;\n", @@ -157,7 +139,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Http请求方法" + "### Http 请求方法" ] }, { @@ -210,7 +192,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### 发出 http 请求" + "### 使用 HttpClient 发出请求" ] }, { @@ -264,28 +246,81 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## 3、处理 http 响应" + "### Http Get" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## 4、HTTP 有效内容响应" + "GET 请求不应发送正文,而是用于从资源检索数据。 要在给定 HttpClient 和 URI 的情况下发出 HTTP GET 请求,推荐使用 HttpClient.GetAsync 方法。\n", + "\n", + "> Get 请求,一般不会发送数据(服务器支持的话,也能带),最大特点是:可以缓存请求结果,减少服务器压力。" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "//Get 请求\n", + "{\n", + " //使用共享客户端发送Get请求\n", + " var response = await SharedClient.GetAsync(\"/api/hello/index\");\n", + "\n", + " //确保请求成功\n", + " response.EnsureSuccessStatusCode();\n", + "\n", + " //读取响应内容\n", + " var content = await response.Content.ReadAsStringAsync();\n", + "\n", + " //输出 响应内容\n", + " Console.WriteLine(content);\n", + "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## 5、HTTP 错误处理" + "## Http Get Json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## 6、Http 代理" + "System.Net.Http.Json 库,为请求Json格式数据和接受json格式数据提供了很多扩展方法,使用 HttpClient 发送和请求Json格式数据非常方便; \n", + "后面单独章节讲解,这里只是一个例子." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "/*\n", + " Get Json\n", + " 需要先引入 System.Net.Http.Json 和 命名空间(全局设置里已完成)\n", + "*/\n", + "{\n", + "\n", + " //使用共享客户端发送Get请求\n", + " var content = await SharedClient.GetFromJsonAsync<HttpClientStudy.Model.Account>(\"/api/hello/GetAccount\");\n", + "\n", + " //框架显示方法\n", + " content.Display();\n", + "}" ] } ], diff --git a/Docs/Assets/debug-visual-studio.png b/Docs/Assets/debug-visual-studio.png new file mode 100644 index 0000000..7136a2c Binary files /dev/null and b/Docs/Assets/debug-visual-studio.png differ diff --git a/HttpClientStudy.Core/runtimes/win/lib/net8.0/System.Diagnostics.EventLog.Messages.dll b/HttpClientStudy.Core/runtimes/win/lib/net8.0/System.Diagnostics.EventLog.Messages.dll deleted file mode 100644 index a8d91f3..0000000 Binary files a/HttpClientStudy.Core/runtimes/win/lib/net8.0/System.Diagnostics.EventLog.Messages.dll and /dev/null differ diff --git a/HttpClientStudy.Core/runtimes/win/lib/net8.0/System.Diagnostics.EventLog.dll b/HttpClientStudy.Core/runtimes/win/lib/net8.0/System.Diagnostics.EventLog.dll deleted file mode 100644 index 33066bd..0000000 Binary files a/HttpClientStudy.Core/runtimes/win/lib/net8.0/System.Diagnostics.EventLog.dll and /dev/null differ diff --git a/HttpClientStudy.Core/runtimes/win/lib/net8.0/System.Security.Cryptography.Pkcs.dll b/HttpClientStudy.Core/runtimes/win/lib/net8.0/System.Security.Cryptography.Pkcs.dll deleted file mode 100644 index cba73d0..0000000 Binary files a/HttpClientStudy.Core/runtimes/win/lib/net8.0/System.Security.Cryptography.Pkcs.dll and /dev/null differ diff --git a/HttpClientStudy.WebApp/Controllers/HelloController .cs b/HttpClientStudy.WebApp/Controllers/HelloController .cs index 594e0b3..23e8f5f 100644 --- a/HttpClientStudy.WebApp/Controllers/HelloController .cs +++ b/HttpClientStudy.WebApp/Controllers/HelloController .cs @@ -50,6 +50,27 @@ namespace HttpClientStudy.WebApp.Controllers return Ok("Hello, world!"); } + /// <summary> + /// Get请求 + /// </summary> + /// <returns> + /// json对象 + /// </returns> + [HttpGet] + public IActionResult GetAccount() + { + var account = new Account() + { + Id = 1, + Name = "Hello", + Password = "pwd", + Role = "Devlopment" + + }; + + return new JsonResult(account); + } + /// <summary> /// Post 请求 /// </summary> diff --git a/HttpClientStudy.WebApp/appsettings.json b/HttpClientStudy.WebApp/appsettings.json index d96cb05..c0de336 100644 --- a/HttpClientStudy.WebApp/appsettings.json +++ b/HttpClientStudy.WebApp/appsettings.json @@ -1,5 +1,5 @@ { - "urls": "http://192.168.20.46:5189", + "urls": "http://*:5189", "Logging": { "LogLevel": { "Default": "Information",