core 调用webservices_.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> Get()

{var url = @"http://127.0.0.1:8888/demo/test.asmx/save";

Dictionary dicParam = new Dictionary();

dicParam.Add("id", "1");

dicParam.Add("name", "张三");

HttpContent content = new FormUrlEncodedContent(dicParam);return await RemoteHelper(url, content);

}private async Task 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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值