NETCORE - 調用第三方接口
調用方式: WebApi
創建兩個 NetCore WebApi 項目 ,框架:.net5
1) 項目:NETCORE.ConSul
2) 項目:NETCORE.ConSul.Service
一. 項目 NETCORE.ConSul.Service
1. 新建接口
namespace NETCORE.Consul.Service.Controllers { [Route("api/[controller]")] [ApiController] public class PenController : ControllerBase { //測試接口 [Route("GetPenInfo")] [HttpGet] public string GetPenInfo() { Task.Run(() => { Console.WriteLine($" get a new pen , Host: {0}", HttpContext.Request.Host.Value); }); return "success! get a new pen !"; } } }
2. 修改IP地址與端口:
路徑:Properties / launchSettings.json
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:24384", "sslPort": 44335 } }, "$schema": "http://json.schemastore.org/launchsettings.json", "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "NETCORE.Consul.Service": { "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "dotnetRunMessages": "true", "applicationUrl": "https://10.10.50.99:5055;http://10.10.50.99:5056", "jsWebView2Debugging": true } } }
3. 調試
接口訪問地址:https://10.10.50.99:5055/api/Pen/GetPenInfo
二. 項目 NETCORE.Consul
1. 修改IP地址與端口:
路徑:Properties / launchSettings.json
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://10.10.50.99:19691", "sslPort": 44328 } }, "$schema": "http://json.schemastore.org/launchsettings.json", "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "NETCORE.ConSul": { "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "dotnetRunMessages": "true", "applicationUrl": "https://10.10.50.99:5001;http://10.10.50.99:5000" } } }
2. 注入 HTTPClient
在 startup.cs 中修改 ConfigureServices 方法
public void ConfigureServices(IServiceCollection services) { //用於調用博客園接口 services.AddHttpClient("ServiceBlogs", x => { x.BaseAddress = new Uri("https://www.cnblogs.com"); x.DefaultRequestHeaders.Add("Cache-Control", "no-cache"); }); //用於調用Service1接口 services.AddHttpClient("ServiceService1", x => { x.BaseAddress = new Uri("https://10.10.50.99:5055"); x.DefaultRequestHeaders.Add("Cache-Control", "no-cache"); }).ConfigurePrimaryHttpMessageHandler(() =>//防止報ssl錯誤 { return new HttpClientHandler() { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator }; });
services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "NETCORE.ConSul", Version = "v1" }); }); }
3. 新增接口
namespace NETCORE.ConSul.Controllers { [Route("api/[controller]")] [ApiController] public class BookController : ControllerBase { IHttpClientFactory _clientFactory; public BookController(IHttpClientFactory clientFactory) { _clientFactory = clientFactory; } [Route("CallPen")] [HttpPost] public async Task<IActionResult> CallPen() { var client = _clientFactory.CreateClient("ServiceService1");//必須與startup里面注入的名稱對應 //var content = new StringContent(""); //content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); var response = await client.GetAsync("api/pen/GetPenInfo"); var sss = response.Content.ReadAsStringAsync().Result; return Ok(sss); } } }
4. 調用接口
項目:NETCORE.ConSul
項目:NETCORE.ConSulService
附代碼:https://gitee.com/wuxincaicai/NETCORE.git