李昊 il y a 4 ans
Parent
commit
92eb62d689

+ 44 - 0
GxPress/Infrastructure/GxPress.Common/Http/HttpClientHelper.cs

@@ -1,5 +1,8 @@
 using System.Collections.Generic;
+using System.IO;
+using System.Net;
 using System.Net.Http;
+using System.Text;
 using System.Threading.Tasks;
 
 namespace GxPress.Common.Http
@@ -81,5 +84,46 @@ namespace GxPress.Common.Http
             HttpResponseMessage response = await httpClient.DeleteAsync(url);
             return response;
         }
+
+        /// <summary>
+        /// 返回JSon数据
+        /// </summary>
+        /// <param name="JSONData">要处理的JSON数据</param>
+        /// <param name="Url">要提交的URL</param>
+        /// <returns>返回的JSON处理字符串</returns>
+        public static string GetResponseData(string JSONData, string Url)
+        {
+            try
+            {
+                byte[] bytes = Encoding.UTF8.GetBytes(JSONData);
+                var request = (HttpWebRequest)WebRequest.Create(Url);
+                request.Method = "POST";
+                request.ContentLength = bytes.Length;
+                request.ContentType = "text/xml";
+                Stream reqstream = request.GetRequestStream();
+                reqstream.Write(bytes, 0, bytes.Length);
+
+                //声明一个HttpWebRequest请求
+                request.Timeout = 90000;
+                //设置连接超时时间
+                //request.Headers.Set("Pragma", "no-cache");
+                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
+                Stream streamReceive = response.GetResponseStream();
+                Encoding encoding = Encoding.UTF8;
+
+                StreamReader streamReader = new StreamReader(streamReceive, encoding);
+                string strResult = streamReader.ReadToEnd();
+                streamReceive.Dispose();
+                streamReader.Dispose();
+
+                return strResult;
+            }
+            catch (System.Exception)
+            {
+
+                return "";
+            }
+            return "";
+        }
     }
 }

+ 21 - 16
GxPress/Repository/GxPress.Repository.Implement/UserRepository.cs

@@ -40,14 +40,14 @@ namespace GxPress.Repository.Implement
         private readonly IMapper _mapper;
         private readonly IWebHostEnvironment _environment;
 
-         private readonly ILogger<UserRepository> _logger;
+        private readonly ILogger<UserRepository> _logger;
         private readonly IDistributedCache _cache;
         //private readonly string _connectionString;
         //private readonly string _databaseTypestr;
         private readonly string _connectionString;
         private readonly string _databaseTypeStr;
         public UserRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper,
-            IWebHostEnvironment environment, IDistributedCache cache,ILogger<UserRepository> logger)
+            IWebHostEnvironment environment, IDistributedCache cache, ILogger<UserRepository> logger)
         {
             _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
             _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
@@ -63,7 +63,7 @@ namespace GxPress.Repository.Implement
             _roleRepository = new Repository<Role>(database);
             _mapper = mapper;
             _cache = cache;
-            _logger=logger;
+            _logger = logger;
         }
 
         public IDatabase Database => _repository.Database;
@@ -661,15 +661,19 @@ namespace GxPress.Repository.Implement
                 // {
                 //     ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true
                 // };
-                HttpClientHandler clientHandler = new HttpClientHandler();
-                clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
-                HttpClient httpClient = new HttpClient(clientHandler); //http对象
-                HttpResponseMessage response = await httpClient.PostAsync(
-                    $"{imServiceUrl}/users",
-                    new JsonContent(new { username = request.Phone, password = "123456", nickname = request.Name }));
-                if (response.IsSuccessStatusCode)
+                // HttpClientHandler clientHandler = new HttpClientHandler();
+                // clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
+                // HttpClient httpClient = new HttpClient(clientHandler); //http对象
+                // HttpResponseMessage response = await httpClient.PostAsync(
+                //     $"{imServiceUrl}/users",
+                //     new JsonContent(new { username = request.Phone, password = "123456", nickname = request.Name }));
+
+                string result = HttpClientHelper.GetResponseData("{\"username\":\"" + request.Phone + "\", \"password\": \"123456\",\"nickname\":\"" + request.Name + "\"}", $"{imServiceUrl}/users");
+                // if (response.IsSuccessStatusCode)
+                // {
+                //string result = await response.Content.ReadAsStringAsync();
+                if (!string.IsNullOrEmpty(result))
                 {
-                    string result = await response.Content.ReadAsStringAsync();
                     _logger.LogDebug(result);
                     _logger.LogError(result);
                     var model = JsonConvert.DeserializeObject<dynamic>(result);
@@ -682,11 +686,12 @@ namespace GxPress.Repository.Implement
                     await _repository.UpdateAsync(userEntity);
                     Thread.Sleep(1000);
                 }
-                else
-                {
-                    //出现请求环信失败
-                    // throw new BusinessException("创建用户失败");
-                }
+                // }
+                // else
+                // {
+                //     //出现请求环信失败
+                //     // throw new BusinessException("创建用户失败");
+                // }
             }
             catch (Exception e)
             {