zoukankan      html  css  js  c++  java
  • .Net Core调用第三方WebService

    本示例使用的是.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;
            }
        }
    }
  • 相关阅读:
    phpstorm操作集锦
    图片、音频获取二进制流或url的blob值
    sublime text 3 快捷键
    dd与sql 打印工具
    php生成二维码(可带logo)
    jQuery append加入的元素 绑定事件无效
    Linux运维架构师学习之路
    硬盘安装win7
    Composer安装与使用
    Js循环做法
  • 原文地址:https://www.cnblogs.com/az4215/p/12467308.html
Copyright © 2011-2022 走看看