当前位置: 移动技术网 > IT编程>开发语言>.net > .Net Core调用第三方WebService

.Net Core调用第三方WebService

2020年03月12日  | 移动技术网IT编程  | 我要评论

洱吧下载,冯小刚买菜狂砍价,邪恶力量第九季

本示例使用的是.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;
        }
    }
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网