黄贺彬 6 years ago
parent
commit
1fa1437d6b

+ 2 - 1
TEAMModelOS.SDK/Extension/HttpClient/HttpClientExtension.cs

@@ -9,7 +9,8 @@ namespace TEAMModelOS.SDK.Extension.HttpClient
         {
         {
             //services.AddHttpClient();
             //services.AddHttpClient();
             //services.AddSingleton<HttpClientService>();
             //services.AddSingleton<HttpClientService>();
-            services.AddHttpClient<HttpClientService>();
+            services.AddHttpClient<HttpClientSendCloud>();
+            services.AddHttpClient<HttpClientUserInfo>();
         }
         }
     }
     }
 }
 }

+ 40 - 37
TEAMModelOS.SDK/Extension/HttpClient/Implements/HttpClientService.cs

@@ -3,6 +3,8 @@ using System.Collections.Generic;
 using System.Net.Http;
 using System.Net.Http;
 using System.Text;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
+using TEAMModelOS.SDK.Context.Configuration;
+using TEAMModelOS.SDK.Context.Constant.Common;
 using HttpClientSpace = System.Net.Http;
 using HttpClientSpace = System.Net.Http;
 
 
 namespace TEAMModelOS.SDK.Extension.HttpClient.Implements
 namespace TEAMModelOS.SDK.Extension.HttpClient.Implements
@@ -10,11 +12,12 @@ namespace TEAMModelOS.SDK.Extension.HttpClient.Implements
     /// <summary>
     /// <summary>
     /// 需要调整的  https://blog.yowko.com/httpclientfactory-dotnet-core-dotnet-framework/
     /// 需要调整的  https://blog.yowko.com/httpclientfactory-dotnet-core-dotnet-framework/
     /// </summary>
     /// </summary>
-    public class HttpClientService
+    public class HttpClientSendCloud
     {
     {
         HttpClientSpace.HttpClient client { get; }
         HttpClientSpace.HttpClient client { get; }
-        public HttpClientService(HttpClientSpace.HttpClient _client)
+        public HttpClientSendCloud(HttpClientSpace.HttpClient _client)
         {
         {
+           // _client.DefaultRequestHeaders.Add(Constants.AUTHORIZATION, BaseConfigModel.Configuration["SmsSendCloud:UserInfoKey"]);
             client = _client;
             client = _client;
         }
         }
 
 
@@ -28,13 +31,13 @@ namespace TEAMModelOS.SDK.Extension.HttpClient.Implements
         public string HttpGet(string url, Dictionary<string, string> headers = null, int timeout = 0)
         public string HttpGet(string url, Dictionary<string, string> headers = null, int timeout = 0)
         {
         {
 
 
-            if (headers != null)
-            {
-                foreach (KeyValuePair<string, string> header in headers)
-                {
-                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
-                }
-            }
+            //if (headers != null)
+            //{
+            //    foreach (KeyValuePair<string, string> header in headers)
+            //    {
+            //        client.DefaultRequestHeaders.Add(header.Key, header.Value);
+            //    }
+            //}
             if (timeout > 0)
             if (timeout > 0)
             {
             {
                 client.Timeout = new TimeSpan(0, 0, timeout);
                 client.Timeout = new TimeSpan(0, 0, timeout);
@@ -52,13 +55,13 @@ namespace TEAMModelOS.SDK.Extension.HttpClient.Implements
         /// <returns></returns>
         /// <returns></returns>
         public async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null, int timeout = 0)
         public async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null, int timeout = 0)
         {
         {
-            if (headers != null)
-            {
-                foreach (KeyValuePair<string, string> header in headers)
-                {
-                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
-                }
-            }
+            //if (headers != null)
+            //{
+            //    foreach (KeyValuePair<string, string> header in headers)
+            //    {
+            //        client.DefaultRequestHeaders.Add(header.Key, header.Value);
+            //    }
+            //}
             if (timeout > 0)
             if (timeout > 0)
             {
             {
                 client.Timeout = new TimeSpan(0, 0, timeout);
                 client.Timeout = new TimeSpan(0, 0, timeout);
@@ -80,13 +83,13 @@ namespace TEAMModelOS.SDK.Extension.HttpClient.Implements
         /// <returns></returns>
         /// <returns></returns>
         public string HttpPost(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
         public string HttpPost(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
         {
         {
-            if (headers != null)
-            {
-                foreach (KeyValuePair<string, string> header in headers)
-                {
-                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
-                }
-            }
+            //if (headers != null)
+            //{
+            //    foreach (KeyValuePair<string, string> header in headers)
+            //    {
+            //        client.DefaultRequestHeaders.Add(header.Key, header.Value);
+            //    }
+            //}
             if (timeout > 0)
             if (timeout > 0)
             {
             {
                 client.Timeout = new TimeSpan(0, 0, timeout);
                 client.Timeout = new TimeSpan(0, 0, timeout);
@@ -114,13 +117,13 @@ namespace TEAMModelOS.SDK.Extension.HttpClient.Implements
         /// <returns></returns>
         /// <returns></returns>
         public async Task<string> HttpPostAsync(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
         public async Task<string> HttpPostAsync(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
         {
         {
-            if (headers != null)
-            {
-                foreach (KeyValuePair<string, string> header in headers)
-                {
-                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
-                }
-            }
+            //if (headers != null)
+            //{
+            //    foreach (KeyValuePair<string, string> header in headers)
+            //    {
+            //        client.DefaultRequestHeaders.Add(header.Key, header.Value);
+            //    }
+            //}
             if (timeout > 0)
             if (timeout > 0)
             {
             {
                 client.Timeout = new TimeSpan(0, 0, timeout);
                 client.Timeout = new TimeSpan(0, 0, timeout);
@@ -148,13 +151,13 @@ namespace TEAMModelOS.SDK.Extension.HttpClient.Implements
         /// <returns></returns>
         /// <returns></returns>
         public async Task<string> HttpPostAsync(string url, List<KeyValuePair<string, string>> postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
         public async Task<string> HttpPostAsync(string url, List<KeyValuePair<string, string>> postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
         {
         {
-            if (headers != null)
-            {
-                foreach (KeyValuePair<string, string> header in headers)
-                {
-                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
-                }
-            }
+            //if (headers != null)
+            //{
+            //    foreach (KeyValuePair<string, string> header in headers)
+            //    {
+            //        client.DefaultRequestHeaders.Add(header.Key, header.Value);
+            //    }
+            //}
             if (timeout > 0)
             if (timeout > 0)
             {
             {
                 client.Timeout = new TimeSpan(0, 0, timeout);
                 client.Timeout = new TimeSpan(0, 0, timeout);

+ 177 - 0
TEAMModelOS.SDK/Extension/HttpClient/Implements/HttpClientUserInfo.cs

@@ -0,0 +1,177 @@
+using System;
+using System.Collections.Generic;
+using System.Net.Http;
+using System.Text;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.Context.Configuration;
+using TEAMModelOS.SDK.Context.Constant.Common;
+using HttpClientSpace = System.Net.Http;
+
+namespace TEAMModelOS.SDK.Extension.HttpClient.Implements
+{
+    /// <summary>
+    /// 需要调整的  https://blog.yowko.com/httpclientfactory-dotnet-core-dotnet-framework/
+    /// </summary>
+    public class HttpClientUserInfo
+    {
+        HttpClientSpace.HttpClient client { get; }
+        public HttpClientUserInfo(HttpClientSpace.HttpClient _client)
+        {
+            // _client.BaseAddress =new Uri(BaseConfigModel.Configuration["HaBookAuth:AccountUrl"]);
+            _client.Timeout = new TimeSpan(0, 0, 1000);
+            _client.DefaultRequestHeaders.Add(Constants.AUTHORIZATION, BaseConfigModel.Configuration["HaBookAuth:UserInfoKey"]);
+            client = _client;
+        }
+
+        /// <summary>
+        /// 同步GET请求
+        /// </summary>
+        /// <param name="url"></param>
+        /// <param name="headers"></param>
+        /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
+        /// <returns></returns>
+        public string HttpGet(string url)
+        {
+
+            //if (headers != null)
+            //{
+            //    foreach (KeyValuePair<string, string> header in headers)
+            //    {
+            //        client.DefaultRequestHeaders.Add(header.Key, header.Value);
+            //    }
+            //}
+            //if (timeout > 0)
+            //{
+            //    client.Timeout = new TimeSpan(0, 0, timeout);
+            //}
+            byte[] resultBytes = client.GetByteArrayAsync(url).Result;
+            return Encoding.UTF8.GetString(resultBytes);
+        }
+
+        /// <summary>
+        /// 异步GET请求
+        /// </summary>
+        /// <param name="url"></param>
+        /// <param name="headers"></param>
+        /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
+        /// <returns></returns>
+        public async Task<string> HttpGetAsync(string url)
+        {
+            //if (headers != null)
+            //{
+            //    foreach (KeyValuePair<string, string> header in headers)
+            //    {
+            //        client.DefaultRequestHeaders.Add(header.Key, header.Value);
+            //    }
+            //}
+            //if (timeout > 0)
+            //{
+            //    client.Timeout = new TimeSpan(0, 0, timeout);
+            //}
+            byte[] resultBytes = await client.GetByteArrayAsync(url);
+            return Encoding.Default.GetString(resultBytes);
+        }
+
+
+        /// <summary>
+        /// 同步POST请求
+        /// </summary>
+        /// <param name="url"></param>
+        /// <param name="postData"></param>
+        /// <param name="headers"></param>
+        /// <param name="contentType"></param>
+        /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
+        /// <param name="encoding">默认UTF8</param>
+        /// <returns></returns>
+        public string HttpPost(string url, string postData,  string contentType = null, Encoding encoding = null)
+        {
+            //if (headers != null)
+            //{
+            //    foreach (KeyValuePair<string, string> header in headers)
+            //    {
+            //        client.DefaultRequestHeaders.Add(header.Key, header.Value);
+            //    }
+            //}
+            //if (timeout > 0)
+            //{
+            //    client.Timeout = new TimeSpan(0, 0, timeout);
+            //}
+            HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8);
+
+            if (contentType != null)
+            {
+                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
+            }
+            HttpResponseMessage responseMessage = client.PostAsync(url, content).Result;
+            byte[] resultBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
+            return Encoding.UTF8.GetString(resultBytes);
+        }
+
+        /// <summary>
+        /// 异步POST请求
+        /// </summary>
+        /// <param name="url"></param>
+        /// <param name="postData"></param>
+        /// <param name="headers"></param>
+        /// <param name="contentType"></param>
+        /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
+        /// <param name="encoding">默认UTF8</param>
+        /// <returns></returns>
+        public async Task<string> HttpPostAsync(string url, string postData, string contentType = null, Encoding encoding = null)
+        {
+            //if (headers != null)
+            //{
+            //    foreach (KeyValuePair<string, string> header in headers)
+            //    {
+            //        client.DefaultRequestHeaders.Add(header.Key, header.Value);
+            //    }
+            //}
+            //if (timeout > 0)
+            //{
+            //    client.Timeout = new TimeSpan(0, 0, timeout);
+            //}
+            HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8);
+
+            if (contentType != null)
+            {
+                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
+            }
+            HttpResponseMessage responseMessage = await client.PostAsync(url, content);
+            byte[] resultBytes = await responseMessage.Content.ReadAsByteArrayAsync();
+            return Encoding.UTF8.GetString(resultBytes);
+        }
+
+        /// <summary>
+        /// 异步POST请求
+        /// </summary>
+        /// <param name="url"></param>
+        /// <param name="postData"></param>
+        /// <param name="headers"></param>
+        /// <param name="contentType"></param>
+        /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
+        /// <param name="encoding">默认UTF8</param>
+        /// <returns></returns>
+        public async Task<string> HttpPostAsync(string url, List<KeyValuePair<string, string>> postData,  string contentType = null, Encoding encoding = null)
+        {
+            //if (headers != null)
+            //{
+            //    foreach (KeyValuePair<string, string> header in headers)
+            //    {
+            //        client.DefaultRequestHeaders.Add(header.Key, header.Value);
+            //    }
+            //}
+            //if (timeout > 0)
+            //{
+            //    client.Timeout = new TimeSpan(0, 0, timeout);
+            //}
+            HttpContent content = new FormUrlEncodedContent(postData);
+            if (contentType != null)
+            {
+                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
+            }
+            HttpResponseMessage responseMessage = await client.PostAsync(url, content);
+            byte[] resultBytes = await responseMessage.Content.ReadAsByteArrayAsync();
+            return Encoding.UTF8.GetString(resultBytes);
+        }
+    }
+}

+ 2 - 2
TEAMModelOS.SDK/Extension/MessagePush/Implements/SendCloudService.cs

@@ -20,8 +20,8 @@ namespace TEAMModelOS.SDK.Extension.MessagePush.Implements
         public SmsSendCloud smsSendCloud;
         public SmsSendCloud smsSendCloud;
         public ILanguageService _languageService;
         public ILanguageService _languageService;
         public IAzureTableDBRepository _azureTableDBRepository;
         public IAzureTableDBRepository _azureTableDBRepository;
-        public HttpClientService _httpClientService;
-        public SendCloudService(IOptions<SmsSendCloud> _option, ILanguageService languageService , IAzureTableDBRepository azureTableDBRepository ,HttpClientService httpClientService )
+        public HttpClientSendCloud _httpClientService;
+        public SendCloudService(IOptions<SmsSendCloud> _option, ILanguageService languageService , IAzureTableDBRepository azureTableDBRepository , HttpClientSendCloud httpClientService )
         {
         {
             _azureTableDBRepository = azureTableDBRepository;
             _azureTableDBRepository = azureTableDBRepository;
             _languageService = languageService;
             _languageService = languageService;

+ 4 - 7
TEAMModelOS.Service/Common/Implements/LoginInfoService.cs

@@ -31,8 +31,8 @@ namespace TEAMModelOS.Service.Common.Implements
         private IAzureTableDBRepository _repository;
         private IAzureTableDBRepository _repository;
         private IOptions<JwtSetting> _options;
         private IOptions<JwtSetting> _options;
         private IHttpContextAccessor _httpContextAccessor;
         private IHttpContextAccessor _httpContextAccessor;
-        private HttpClientService _httpClientService;
-        public LoginInfoService(IAzureTableDBRepository repository, IOptions<JwtSetting> options, IHttpContextAccessor httpContextAccessor  , HttpClientService httpClientService)
+        private HttpClientUserInfo _httpClientService;
+        public LoginInfoService(IAzureTableDBRepository repository, IOptions<JwtSetting> options, IHttpContextAccessor httpContextAccessor  , HttpClientUserInfo httpClientService)
         {
         {
             _httpContextAccessor = httpContextAccessor;
             _httpContextAccessor = httpContextAccessor;
             _options = options;
             _options = options;
@@ -59,10 +59,7 @@ namespace TEAMModelOS.Service.Common.Implements
                     await _repository.Update<LoginInfo>(login);
                     await _repository.Update<LoginInfo>(login);
                     return result;
                     return result;
                 }
                 }
-                Dictionary<string, string> dict = new Dictionary<string, string>
-                {
-                    { Constants.AUTHORIZATION, BaseConfigModel.Configuration["HaBookAuth:UserInfoKey"] }
-                };
+               
                 JosnRPCRequest<Dictionary<string, object>> request = new JosnRPCRequest<Dictionary<string, object>>
                 JosnRPCRequest<Dictionary<string, object>> request = new JosnRPCRequest<Dictionary<string, object>>
                 {
                 {
                     method = "UserInfo"
                     method = "UserInfo"
@@ -77,7 +74,7 @@ namespace TEAMModelOS.Service.Common.Implements
                 };
                 };
                 request.@params = ticket;
                 request.@params = ticket;
                 string data = MessagePackHelper.ObjectToJson(request);
                 string data = MessagePackHelper.ObjectToJson(request);
-                string jsonStr = _httpClientService.HttpPost(BaseConfigModel.Configuration["HaBookAuth:AccountUrl"], data, dict, Constants.CONTENT_TYPE_JSON, 1000, Encoding.UTF8);
+                string jsonStr = _httpClientService.HttpPost(BaseConfigModel.Configuration["HaBookAuth:AccountUrl"], data,  Constants.CONTENT_TYPE_JSON, Encoding.UTF8);
                 if (!string.IsNullOrEmpty(jsonStr))
                 if (!string.IsNullOrEmpty(jsonStr))
                 {
                 {
                     JosnRPCResponse<TeamModelIdInfo> response = MessagePackHelper.JsonToObject<JosnRPCResponse<TeamModelIdInfo>>(jsonStr);
                     JosnRPCResponse<TeamModelIdInfo> response = MessagePackHelper.JsonToObject<JosnRPCResponse<TeamModelIdInfo>>(jsonStr);

+ 1 - 1
TEAMModelOS/Controllers/Common/LoginController.cs

@@ -74,7 +74,7 @@ namespace TEAMModelOS.Controllers.Common
         [AllowAnonymous]
         [AllowAnonymous]
         public async Task<BaseJosnRPCResponse> Login(JosnRPCRequest<Dictionary<string,object>> request)
         public async Task<BaseJosnRPCResponse> Login(JosnRPCRequest<Dictionary<string,object>> request)
         {
         {
-            string id = HttpContextHelper.GetLoginUser(_httpContextAccessor);
+            //string id = HttpContextHelper.GetLoginUser(_httpContextAccessor);
             JsonRPCResponseBuilder responseBuilder = JsonRPCResponseBuilder.custom();
             JsonRPCResponseBuilder responseBuilder = JsonRPCResponseBuilder.custom();
             responseBuilder.Data(BaseConfigModel.Configuration["HaBookAuth:TeamModelLoginUrl"]);
             responseBuilder.Data(BaseConfigModel.Configuration["HaBookAuth:TeamModelLoginUrl"]);
             return await Task.Factory.StartNew(() => { return responseBuilder.build(); });
             return await Task.Factory.StartNew(() => { return responseBuilder.build(); });