1、在appsettings.json中配置第三方应用服务地址
“OtherServerConfig”: {
“ApplicationServerUrl”: “http://192.168.199.111:8017”, //第三方应用服务地址
}
2、Startup.cs文件中配置
代码:
services.AddHttpClient("OtherApplicationServer", c =>
{
c.BaseAddress = new Uri(_appConfiguration["OtherServerConfig:ApplicationServerUrl"]);//地址从配置文件appsettings.json里取
c.Timeout = TimeSpan.FromSeconds(30);//超时间时间设置为30秒
});
3、客户端请求封装接口
参考下图,
第一步:创建接口类:IOtherHttpClient
代码如下:
///
/// 客户端请求封装接口
///
public interface IHttpClient
{
///
///
///
///
///
///
///
///
public Task<T> GetData<T>(HttpClient client, string action, dynamic param);
///
///
///
///
///
///
///
///
public Task<T> PostData<T>(HttpClient client, string action, dynamic param);
///
///
///
///
///
///
///
///
public Task<T> PutData<T>(HttpClient client, string action, dynamic param);
}
第二步:创建实现类:OtherHttpClient
代码如下:
///
/// 应用服务类
///
public class OtherHttpClient : IOtherHttpClient, ITransientDependency
{
///
/// Get请求获取
///
///
///
///
///
///
public async Task<T> GetData<T>(HttpClient client, string action, dynamic param)
{
try
{
HttpResponseMessage response;
using (HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(param), Encoding.UTF8))
{
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
client.DefaultRequestHeaders.Add("x-token", "M3Q_mQEwmn9l4Ly7fM7DxaMSdlYSSup5lfKLztF");
response = await client.GetAsync(action).ContinueWith(res =>
{
return res;
}).Result;
}
if (response != null && response.IsSuccessStatusCode)
{
return await response.Content.ReadAsAsync<T>();
}
else
{
return default(T);
}
}
catch (Exception ex)
{
throw;
}
finally
{
}
}
///
/// Post请求获
///
/// 返回值类型
///
///
/// 参数
///
public async Task<T> PostData<T>(HttpClient client, string action, dynamic param)
{
try
{
HttpResponseMessage response;
using (HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(param), Encoding.UTF8))
{
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
client.DefaultRequestHeaders.Add("x-token", "M3Q_mQEwmn9l4Ly7fM7DxaMSdlYSSup5lfKLztF_VP97Urf");
response = await client.PostAsync(action, httpContent).ContinueWith(res =>
{
return res;
}).Result;
}
if (response != null && response.IsSuccessStatusCode)
{
return await response.Content.ReadAsAsync<T>();
}
else
{
return default(T);
}
}
catch (Exception ex)
{
throw;
}
finally
{
}
}
///
/// Put请求
///
/// 返回值类型
///
///
/// 参数
///
public async Task<T> PutData<T>(HttpClient client, string action, dynamic param)
{
try
{
HttpResponseMessage response;
using (HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(param), Encoding.UTF8))
{
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
client.DefaultRequestHeaders.Add("x-token", "M3Q_mQEwmn9l4Ly7fM7DxaMSdlYSSup5lfKLztF_VP97Urf");
response = await client.PutAsync(action, httpContent).ContinueWith(res =>
{
return res;
}).Result;
}
if (response != null && response.IsSuccessStatusCode)
{
return await response.Content.ReadAsAsync<T>();
}
else
{
return default(T);
}
}
catch (Exception ex)
{
throw;
}
finally
{
}
}
4、接口中调用方式如下:
先引入:
private IOtherHttpClient _OtherHttpClient;
private readonly IHttpClientFactory _clientFactory;
HttpClient _applicationClient;
public SupApplyAppService(IRepository<upervision, string> repository,
IHttpClientFactory clientFactory,
IOtherHttpClient OtherAHttpClient
) : base(repository)
{
_repository = repository;
_clientFactory = clientFactory;
_applicationClient = _clientFactory.CreateClient("OtherApplicationServer");
_OtherHttpClient = OtherHttpClient;
}
调用封装好的方法:
public async Task<bool> CreateInfo(SupervisionWFDto input)
{
Task<bool> isSuccess = Task.FromResult<bool>(false);
try
{
//调用第三方接口
await _OtherHttpClient.PostData<ResultDto<List<WorkOutDto>>>(_applicationClient, $"/jaxrs/work/process/{input.ProcessInput.ProcessGuid}", input.InParam)
.ContinueWith(res =>
{
if (res.Result.type == "success")
{
isSuccess = Task.Factory.StartNew(() =>
{
//新增数据
base.Create(input.Input);
return true;
});
}
}
return res;
});
}
catch (Exception e)
{
throw new UserFriendlyException(e.Message);
}
return isSuccess.Result ? true : false;
}