Browse Source

完善http-post泛型方法,添加Service工具父类(我爱李静平)

yuzhengyang 8 years ago
parent
commit
a677ae2178

+ 49 - 1
Fork.Net/Y.Utils/NetUtils/HttpUtils/HttpTool.cs

@@ -100,7 +100,6 @@ namespace Y.Utils.NetUtils.HttpUtils
         {
         {
             string rs = null;
             string rs = null;
             ServicePointManager.DefaultConnectionLimit = 300;
             ServicePointManager.DefaultConnectionLimit = 300;
-            System.GC.Collect();
             CookieContainer cookieContainer = new CookieContainer();
             CookieContainer cookieContainer = new CookieContainer();
             // 设置提交的相关参数
             // 设置提交的相关参数
             HttpWebRequest request = null;
             HttpWebRequest request = null;
@@ -197,6 +196,55 @@ namespace Y.Utils.NetUtils.HttpUtils
             }
             }
             return rs;
             return rs;
         }
         }
+        public static T PostJson<T>(string url, string param, string encoding = "utf-8")
+        {
+            ServicePointManager.DefaultConnectionLimit = 300;
+            CookieContainer cookieContainer = new CookieContainer();
+            // 设置提交的相关参数
+            HttpWebRequest request = null;
+            HttpWebResponse SendSMSResponse = null;
+            Stream dataStream = null;
+            StreamReader SendSMSResponseStream = null;
+            try
+            {
+                Encoding myEncoding = Encoding.GetEncoding(encoding);
+                request = WebRequest.Create(url) as HttpWebRequest;
+                request.Method = "POST";
+                request.KeepAlive = false;
+                request.ServicePoint.ConnectionLimit = 300;
+                request.AllowAutoRedirect = true;
+                request.Timeout = 10000;
+                request.ReadWriteTimeout = 10000;
+                request.ContentType = "application/json";
+                request.Accept = "application/xml";
+                request.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode("OpenStack"));
+                string strContent = param;
+                byte[] bytes = myEncoding.GetBytes(strContent);
+                request.Proxy = null;
+                request.CookieContainer = cookieContainer;
+                using (dataStream = request.GetRequestStream())
+                {
+                    dataStream.Write(bytes, 0, bytes.Length);
+                }
+                SendSMSResponse = (HttpWebResponse)request.GetResponse();
+                if (SendSMSResponse.StatusCode != HttpStatusCode.RequestTimeout)
+                {
+                    SendSMSResponseStream = new StreamReader(SendSMSResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));
+                    string response = SendSMSResponseStream.ReadToEnd();
+                    T result = JsonConvert.DeserializeObject<T>(response);
+                    return result;
+                }
+            }
+            catch (Exception e) { }
+            finally
+            {
+                if (dataStream != null) dataStream.Close();
+                if (SendSMSResponseStream != null) SendSMSResponseStream.Close();
+                if (SendSMSResponse != null) SendSMSResponse.Close();
+                if (request != null) request.Abort();
+            }
+            return default(T);
+        }
 
 
         /// <summary>
         /// <summary>
         /// http下载文件
         /// http下载文件

+ 21 - 0
Fork.Net/Y.Utils/SoftwareUtils/SoftwareInfo.cs

@@ -7,12 +7,33 @@ namespace Y.Utils.SoftwareUtils
 {
 {
     public class SoftwareInfo
     public class SoftwareInfo
     {
     {
+        /// <summary>
+        /// 软件名称
+        /// </summary>
         public string Name { get; set; }
         public string Name { get; set; }
+        /// <summary>
+        /// 软件版本
+        /// </summary>
         public string Version { get; set; }
         public string Version { get; set; }
+        /// <summary>
+        /// 开发商
+        /// </summary>
         public string Publisher { get; set; }
         public string Publisher { get; set; }
+        /// <summary>
+        /// 帮助链接
+        /// </summary>
         public string HelpLink { get; set; }
         public string HelpLink { get; set; }
+        /// <summary>
+        /// 介绍链接
+        /// </summary>
         public string URLInfoAbout { get; set; }
         public string URLInfoAbout { get; set; }
+        /// <summary>
+        /// 空间占用
+        /// </summary>
         public int EstimatedSize { get; set; }
         public int EstimatedSize { get; set; }
+        /// <summary>
+        /// 安装日期
+        /// </summary>
         public DateTime InstallDate { get; set; }
         public DateTime InstallDate { get; set; }
     }
     }
 }
 }

+ 86 - 0
Fork.Net/Y.Utils/TaskServiceUtils/TaskServiceDaddy.cs

@@ -0,0 +1,86 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Y.Utils.TaskServiceUtils
+{
+    public abstract class TaskServiceDaddy
+    {
+        /// <summary>
+        /// 已启动
+        /// </summary>
+        public bool IsStart { get { return _IsStart; } }
+        /// <summary>
+        /// 已启动(Protect)
+        /// </summary>
+        protected bool _IsStart = false;
+        /// <summary>
+        /// 取消标志
+        /// </summary>
+        protected CancellationTokenSource CT = new CancellationTokenSource();
+        /// <summary>
+        /// 任务循环间隔
+        /// </summary>
+        protected int Interval = 1000;
+
+        private bool IsDestroy = false;
+
+        /// <summary>
+        /// 设置任务间隔(0为不循环任务)
+        /// </summary>
+        /// <param name="i"></param>
+        /// <returns></returns>
+        public int SetInterval(int i)
+        {
+            Interval = i;
+            return Interval;
+        }
+
+        /// <summary>
+        /// 启动服务任务
+        /// </summary>
+        public virtual void Start()
+        {
+            if (!IsDestroy)
+                Task.Factory.StartNew(() =>
+                {
+                    if (!IsStart)
+                    {
+                        _IsStart = true;
+                        BeforeTODO();
+                        do
+                        {
+                            TODO();
+                            Thread.Sleep(Interval);
+
+                        } while (!CT.IsCancellationRequested && Interval > 0);
+                        AfterTODO();
+                    }
+                });
+        }
+        /// <summary>
+        /// 提前干点啥
+        /// </summary>
+        public virtual void BeforeTODO() { }
+        /// <summary>
+        /// 干点啥
+        /// </summary>
+        public abstract void TODO();
+        /// <summary>
+        /// 完事儿干点啥
+        /// </summary>
+        public virtual void AfterTODO() { }
+        /// <summary>
+        /// 停止服务任务
+        /// </summary>
+        public virtual void Stop()
+        {
+            CT.Cancel();
+            IsDestroy = true;
+        }
+
+    }
+}

+ 15 - 0
Fork.Net/Y.Utils/TaskServiceUtils/TestTaskService.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Y.Utils.TaskServiceUtils
+{
+    class TestTaskService : TaskServiceDaddy
+    {
+        public override void TODO()
+        {
+            throw new NotImplementedException();
+        }
+    }
+}

+ 2 - 0
Fork.Net/Y.Utils/Y.Utils.csproj

@@ -88,6 +88,8 @@
     <Compile Include="IOUtils\FileUtils\FileTool.cs" />
     <Compile Include="IOUtils\FileUtils\FileTool.cs" />
     <Compile Include="SoftwareUtils\SoftwareInfo.cs" />
     <Compile Include="SoftwareUtils\SoftwareInfo.cs" />
     <Compile Include="SoftwareUtils\SoftwareTool.cs" />
     <Compile Include="SoftwareUtils\SoftwareTool.cs" />
+    <Compile Include="TaskServiceUtils\TaskServiceDaddy.cs" />
+    <Compile Include="TaskServiceUtils\TestTaskService.cs" />
     <Compile Include="WindowsUtils\APIUtils\FormStyleAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\FormStyleAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\PermissionAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\PermissionAPI.cs" />
     <Compile Include="WindowsUtils\InfoUtils\ClipboardTool.cs" />
     <Compile Include="WindowsUtils\InfoUtils\ClipboardTool.cs" />