using Microsoft.AspNetCore.Hosting;
using Newtonsoft.Json;
using Steeltoe.Common.Discovery;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace CommonUtil.Web
{
///
///
///
public class ServiceUtil
{
private readonly HttpClient client = new HttpClient();
private readonly IHostingEnvironment hostingEnv;
private IDiscoveryClient discClient;
///
///
///
///
///
public ServiceUtil(IDiscoveryClient discoveryClient, IHostingEnvironment hostingEnvironment)
{
DiscoveryHttpClientHandler _handler = new DiscoveryHttpClientHandler(discoveryClient);
client = new HttpClient(_handler, false);
hostingEnv = hostingEnvironment;
discClient = discoveryClient;
}
///
/// Post请求
///
///
///
///
///
///
public async Task PostAsync(ServiceType service, string url, object data) where T : class, new()
{
try
{
string issandbox = ConfigUtil.Configuration["issandbox"] == null ? "" : ConfigUtil.Configuration["issandbox"];
//请求地址
if (hostingEnv.IsDevelopment() || (issandbox == "1"))
{
url = ConfigUtil.Configuration[service.ToString()] + (url.StartsWith("/") ? "" : "/") + url;
}
else
{
IList disservice = discClient.GetInstances(service.ToString());
if (disservice.Count > 0)
{
int index = new Random().Next(0, disservice.Count);
url = "http://" + disservice[index].Host + ":" + disservice[index].Port + (url.StartsWith("/") ? "" : "/") + url;
}
else
{
url = "http://" + service.ToString() + (url.StartsWith("/") ? "" : "/") + url;
}
//url = "http://" + service.ToString() + (url.StartsWith("/") ? "" : "/") + url;
}
string content = "";
if (data is string)
{
content = data.ToString();
}
else
{
content = JsonConvert.SerializeObject(data);
}
ByteArrayContent byteContent = null;
if (service.ToString().StartsWith("wx."))
{
Dictionary dicJsonData = JsonConvert.DeserializeObject>(content);
string postcontent = "";
foreach (string k in dicJsonData.Keys)
{
if (postcontent == "")
{
postcontent = k + "=" + (dicJsonData[k] == null ? "" : dicJsonData[k].ToString());
}
else
{
postcontent += "&" + k + "=" + (dicJsonData[k] == null ? "" : dicJsonData[k].ToString());
}
}
var buffer = Encoding.UTF8.GetBytes(postcontent);
byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
}
else
{
var buffer = Encoding.UTF8.GetBytes(content);
byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
var response = await client.PostAsync(url, byteContent).ConfigureAwait(false);
string result = await response.Content.ReadAsStringAsync();
if (response.StatusCode != HttpStatusCode.OK)
{
return new T();
}
return JsonConvert.DeserializeObject(result);
}
catch (WebException ex)
{
throw new Exception("请求url:" + url + ",错误信息:" + ex.Message);
}
}
///
/// Post请求
///
///
///
///
///
public async Task PostAsync(string url, object data) where T : class, new()
{
try
{
string content = JsonConvert.SerializeObject(data);
var buffer = Encoding.UTF8.GetBytes(content);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var response = await client.PostAsync(url, byteContent).ConfigureAwait(false);
string result = await response.Content.ReadAsStringAsync();
if (response.StatusCode != HttpStatusCode.OK)
{
return new T();
}
return JsonConvert.DeserializeObject(result);
}
catch (WebException ex)
{
if (ex.Response != null)
{
string responseContent = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
throw new System.Exception($"response :{responseContent}", ex);
}
throw;
}
}
///
/// 公共微服务类型
///
public enum ServiceType
{
///
/// 文件服务
///
crmfile,
///
/// MQ服务
///
crmrabbitmq,
///
/// 缓存服务
///
crm_memcache
}
}
}