.Net Core调用第三方WebService
程序员文章站
2023-02-21 15:54:18
本示例使用的是.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 msmq消息队列实例详解
下一篇: C# 打开指定文件或网址
推荐阅读
-
ASP.NET调用WebService服务的方法详解
-
ASP.NET调用WebService服务的方法详解
-
.net core EF Core调用存储过程的方式
-
.net core高吞吐远程方法如何调用组件XRPC详解
-
.NET Core/Framework如何创建委托大幅度提高反射调用的性能详解
-
ASP.NET Core应用中与第三方IoC/DI框架的整合
-
PHP中调用ASP.NET的WebService的代码
-
.net core EF Core调用存储过程的方式
-
.net core高吞吐远程方法如何调用组件XRPC详解
-
ASP.Net Core on Linux (CentOS7) 共享第三方依赖库部署