.Net Core调用第三方WebService
程序员文章站
2022-05-29 13:19:17
本示例使用的是.net core2.2版本,微软提供了访问第三方服务的扩展,只需要在Startup.cs中添加。 紧接着就是通过DI直接使用。示例如下: using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Ge ......
本示例使用的是.net core2.2版本,微软提供了访问第三方服务的扩展,只需要在startup.cs中添加。
紧接着就是通过di直接使用。示例如下:
using microsoft.aspnetcore.mvc; using system; using system.collections.generic; using system.net; using system.net.http; using system.threading.tasks; namespace demo.controllers { [route("api/[controller]")] [apicontroller] public class valuescontroller : controllerbase { private readonly ihttpclientfactory _httpclientfactory; public valuescontroller(ihttpclientfactory httpclientfactory) { _httpclientfactory = httpclientfactory; } [httpget] public async task<actionresult<string>> get() { var url = @"http://127.0.0.1:8888/demo/test.asmx/save"; dictionary<string, string> dicparam = new dictionary<string, string>(); dicparam.add("id", "1"); dicparam.add("name", "张三"); httpcontent content = new formurlencodedcontent(dicparam); return await remotehelper(url, content); } private async task<string> remotehelper(string url, httpcontent content) { var result = string.empty; try { using (var client = _httpclientfactory.createclient()) using (var response = await client.postasync(url, content)) { if (response.statuscode == httpstatuscode.ok) { result = await response.content.readasstringasync(); } } } catch (exception ex) { console.writeline(ex); } return result; } } }
推荐阅读
-
.NET Core 常用第三方包
-
asp.net core IdentityServer4 实现 implicit(隐式许可)实现第三方登录
-
.Net Core调用第三方WebService
-
.Net Core调用oracle存储过程
-
PHP调用.NET的WebService 简单实例
-
.NET Core实战项目之CMS 第十三章 开发篇-在MVC项目结构介绍及应用第三方UI
-
.NET Core 如何调用 WebService
-
Asp.net core调用Earchats,并通过viewbag来进行将后台数据传到前端 原创
-
.NET Core调用WCF的最佳实践
-
Asp.NET Core 如何调用WebService的方法