ソースを参照

课纲保存接口

黄贺彬 6 年 前
コミット
10b4b21682

+ 15 - 0
TEAMModelOS.SDK/Extension/HttpClient/HttpClientExtension.cs

@@ -0,0 +1,15 @@
+using Microsoft.Extensions.DependencyInjection;
+using TEAMModelOS.SDK.Extension.HttpClient.Implements;
+
+namespace TEAMModelOS.SDK.Extension.HttpClient
+{
+   public static class HttpClientExtension
+    {
+        public static void AddHttp(this IServiceCollection services)
+        {
+            //services.AddHttpClient();
+            //services.AddSingleton<HttpClientService>();
+            services.AddHttpClient<HttpClientService>();
+        }
+    }
+}

+ 172 - 0
TEAMModelOS.SDK/Extension/HttpClient/Implements/HttpClientService.cs

@@ -0,0 +1,172 @@
+using System;
+using System.Collections.Generic;
+using System.Net.Http;
+using System.Text;
+using System.Threading.Tasks;
+using HttpClientSpace = System.Net.Http;
+
+namespace TEAMModelOS.SDK.Extension.HttpClient.Implements
+{
+    /// <summary>
+    /// 需要调整的  https://blog.yowko.com/httpclientfactory-dotnet-core-dotnet-framework/
+    /// </summary>
+    public class HttpClientService
+    {
+        HttpClientSpace.HttpClient client { get; }
+        public HttpClientService(HttpClientSpace.HttpClient _client)
+        {
+            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, 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 (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, 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 (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, 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 (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, 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 (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, 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 (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);
+        }
+    }
+}

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

@@ -11,6 +11,7 @@ using TEAMModelOS.SDK.Extension.Language.Model;
 using TEAMModelOS.SDK.Helper.Common.JsonHelper;
 using TEAMModelOS.SDK.Helper.Security.Md5Hash;
 using TEAMModelOS.SDK.Helper.Network.HttpHelper;
+using TEAMModelOS.SDK.Extension.HttpClient.Implements;
 
 namespace TEAMModelOS.SDK.Extension.MessagePush.Implements
 {
@@ -19,11 +20,13 @@ namespace TEAMModelOS.SDK.Extension.MessagePush.Implements
         public SmsSendCloud smsSendCloud;
         public ILanguageService _languageService;
         public IAzureTableDBRepository _azureTableDBRepository;
-        public SendCloudService(IOptions<SmsSendCloud> _option, ILanguageService languageService , IAzureTableDBRepository azureTableDBRepository )
+        public HttpClientService _httpClientService;
+        public SendCloudService(IOptions<SmsSendCloud> _option, ILanguageService languageService , IAzureTableDBRepository azureTableDBRepository ,HttpClientService httpClientService )
         {
             _azureTableDBRepository = azureTableDBRepository;
             _languageService = languageService;
             smsSendCloud = _option.Value;
+            _httpClientService = httpClientService;
         }
 
 
@@ -145,7 +148,7 @@ namespace TEAMModelOS.SDK.Extension.MessagePush.Implements
             string sign_str = smsSendCloud.SmsKey + "&" + param_str + smsSendCloud.SmsKey;
             string sign = Md5Hash.Encrypt(sign_str);
             paramList.Add(new KeyValuePair<string, string>("signature", sign));
-            string result = await HttpHelper.HttpPostAsync(smsSendCloud.SmsUrl, paramList);
+            string result = await _httpClientService.HttpPostAsync(smsSendCloud.SmsUrl, paramList);
             return MessagePackHelper.JsonToObject<SendCloudResponse>(result);
         }
     }

+ 0 - 189
TEAMModelOS.SDK/Helper/Network/HttpHelper/HttpHelper.cs

@@ -1,189 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Net.Http;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace TEAMModelOS.SDK.Helper.Network.HttpHelper
-{
-    /// <summary>
-    /// 需要调整的  https://blog.yowko.com/httpclientfactory-dotnet-core-dotnet-framework/
-    /// </summary>
-    public class HttpHelper
-    {
-        /// <summary>
-        /// 同步GET请求
-        /// </summary>
-        /// <param name="url"></param>
-        /// <param name="headers"></param>
-        /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
-        /// <returns></returns>
-        public static string HttpGet(string url, Dictionary<string, string> headers = null, int timeout = 0)
-        {
-            using (HttpClient client = new HttpClient())
-            {
-                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 static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null, int timeout = 0)
-        {
-            using (HttpClient client = new HttpClient())
-            {
-                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 static string HttpPost(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
-        {
-            using (HttpClient client = new HttpClient())
-            {
-                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);
-                }
-                using (HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8))
-                {
-                    if (contentType != null)
-                    {
-                        content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
-                    }
-                    using (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 static async Task<string> HttpPostAsync(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
-        {
-            using (HttpClient client = new HttpClient())
-            {
-                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);
-                }
-                using (HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8))
-                {
-                    if (contentType != null)
-                    {
-                        content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
-                    }
-                    using (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 static 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)
-        {
-            using (HttpClient client = new HttpClient())
-            {
-                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);
-                }
-                using (HttpContent content = new FormUrlEncodedContent(postData))
-                {
-                    if (contentType != null)
-                    {
-                        content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
-                    }
-                    using (HttpResponseMessage responseMessage = await client.PostAsync(url, content))
-                    {
-                        Byte[] resultBytes = await responseMessage.Content.ReadAsByteArrayAsync();
-                        return Encoding.UTF8.GetString(resultBytes);
-                    }
-                }
-            }
-        }
-    }
-}

+ 1 - 0
TEAMModelOS.SDK/TEAMModelOS.SDK.csproj

@@ -22,6 +22,7 @@
     <PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="2.2.0" />
     <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
     <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
+    <PackageReference Include="Microsoft.Extensions.Http" Version="2.2.0" />
     <PackageReference Include="Microsoft.Extensions.Options" Version="2.2.0" />
     <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
     <PackageReference Include="protobuf-net" Version="2.4.0" />

+ 9 - 2
TEAMModelOS/Controllers/Syllabus/SyllabusController.cs

@@ -2,6 +2,7 @@
 using System.Threading.Tasks;
 using TEAMModelOS.Model.Syllabus.Dtos;
 using TEAMModelOS.SDK.Extension.DataResult.JsonRpcRequest;
+using TEAMModelOS.SDK.Extension.HttpClient.Implements;
 
 namespace TEAMModelOS.Starter.CloudServer.Controllers.Syllabus
 {
@@ -9,10 +10,16 @@ namespace TEAMModelOS.Starter.CloudServer.Controllers.Syllabus
     [ApiController]
     public class SyllabusController: Controller
     {
+        HttpClientService httpClientService;
+        public SyllabusController(HttpClientService _HttpClientService) {
+            httpClientService = _HttpClientService;
+        }
         [HttpPost("SaveOrUpdate")]
-        public Task<BaseJosnRPCRequest> SaveOrUpdate(JosnRPCRequest<SyllabusNode> request )
+        public string SaveOrUpdate(JosnRPCRequest<SyllabusNode> request )
         {
-            return null; 
+
+            string s = httpClientService.HttpGet("https://www.cnblogs.com/AnAng/p/9370913.html");
+            return s; 
         }
     }
 }

+ 4 - 1
TEAMModelOS/Startup.cs

@@ -8,6 +8,7 @@ using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
 using TEAMModelOS.SDK.Context.Configuration;
 using TEAMModelOS.SDK.Context.Filter;
+using TEAMModelOS.SDK.Extension.HttpClient;
 using TEAMModelOS.SDK.Extension.JwtAuth;
 using TEAMModelOS.SDK.Extension.JwtAuth.Filters;
 using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
@@ -19,9 +20,10 @@ namespace TEAMModelOS
 {
     public class Startup
     {
-        public Startup(IConfiguration configuration, IHostingEnvironment env)
+        public Startup(IConfiguration configuration, IHostingEnvironment env )
         {
             Configuration = configuration;
+           
             var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
@@ -63,6 +65,7 @@ namespace TEAMModelOS
             services.JwtAuth(Configuration.GetSection("JwtSetting"));
             //HttpContextAccessor,并用来访问HttpContext。
             services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
+            services.AddHttp();
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.