码迷,mamicode.com
首页 > 其他好文 > 详细

代码分析 公用类库

时间:2014-05-09 17:07:24      阅读:431      评论:0      收藏:0      [点我收藏+]

标签:blog   class   code   tar   color   http   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
公用类库代码 常用方法<br>namespace UtilityLibary
{
    public interface ILog
    {
        int FileSize { get; set; }
 
        /// <summary>
        /// 写入log到指定文件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="fullpath"></param>
        void Log(string msg, string fullpath);
    }
 
    public interface INetHelper
    {
        HttpWebRequest WebRequestInstance { get; set; }
        HttpWebResponse WebResponseInstance { get; set; }
        string Url { get; set; }
    }
 
    public interface IUtility
    {
        /// <summary>
        /// md5 加密 返回信息都是小写
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        string MD5Encrypt(string input);
 
        /// <summary>
        /// 转化时间为时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        long ConvertDateTimeToLong(DateTime dt);
 
        /// <summary>
        /// 转化时间戳为时间
        /// </summary>
        /// <param name="times"></param>
        /// <returns></returns>
        DateTime ConvertLongToDateTime(long times);
 
    }
 
    public interface IFile
    {
        string GetCurrentDirectory();
        string GetSystemDriveDirectory();
        string GetCurrentWorkDirectory();
    }
 
    public abstract class FileBase : IFile
    
        public string GetCurrentDirectory()
        {
            return System.Environment.CurrentDirectory;
        }
 
        public string GetSystemDriveDirectory()
        {
            throw new NotImplementedException();
        }
 
        public string GetCurrentWorkDirectory()
        {
            return System.IO.Directory.GetCurrentDirectory();
        }
    }
 
    public class FileHelper : FileBase
    {
         
    }
 
    public class LogHelper : ILog
    {
        private object mux = new object();
        public int FileSize
        {
            get;
            set;
        }
 
        public void Log(string msg, string fullpath)
        {
            lock (mux)
            {
                if (!File.Exists(fullpath))
                {
                    using (File.Create(fullpath)) { }
                }
                using (StreamWriter sw = new StreamWriter(fullpath))
                {
                    sw.WriteLine(msg);
                }
            }
        }
 
        public void Log(string msg, string directoryPath, bool autoCreateDateDirectory = true)
        {
            lock (mux)
            {
                if (autoCreateDateDirectory)
                {
                    string dateDirectory = Path.Combine(directoryPath, DateTime.Now.ToString("yyyyMMdd"));
                    if (!Directory.Exists(dateDirectory))
                    {
                        Directory.CreateDirectory(directoryPath);
                    }
                    string txtPath = Path.Combine(dateDirectory, "log.txt");
                    if (!File.Exists(txtPath))
                    {
                        using (File.Create(txtPath)) { }
                    }
                    using (StreamWriter sw = new StreamWriter(txtPath, true, Encoding.UTF8))
                    {
                        sw.WriteLine(msg);
                    }
                }
            }
        }
    }
 
    public class NetHelper : INetHelper
    {
        public HttpWebRequest WebRequestInstance
        {
            get;
            set;
        }
 
        public HttpWebResponse WebResponseInstance
        {
            get;
            set;
        }
          
        public string Url
        {
            get;
            set;
        }
 
        public delegate void RequestStreamHandle(HttpWebRequest request);
        /// <summary>
        /// 对请求流做操作
        /// </summary>
        public event RequestStreamHandle RequestStreamEvent;
 
        public delegate void ResponseStreamHandle(HttpWebResponse request);
        /// <summary>
        /// 对返回流做处理
        /// </summary>
        public event ResponseStreamHandle ResponseStreamEvent;
 
        public delegate void UserAgengtHandle(HttpWebRequest request);
        public event UserAgengtHandle UserAgengtEvent;
 
        public NetHelper(string url)
        {
            this.Url = url;
        }
 
        public void GetResponse()
        {
            if (!string.IsNullOrEmpty(Url))
            {
                throw new Exception("Url can‘t be empty");
            }
            WebRequestInstance = WebRequest.Create(this.Url) as HttpWebRequest;
 
            WebRequestInstance.Method = "POST";
            WebRequestInstance.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36";
              
            if (UserAgengtEvent != null)
            {
                UserAgengtEvent(WebRequestInstance);
            }
 
            if (RequestStreamEvent != null)
            {
                RequestStreamEvent(WebRequestInstance);
            }
            try
            {
                WebResponseInstance = WebRequestInstance.GetResponse() as HttpWebResponse;
            }
            catch (Exception ex)
            {
                if (ex is WebException)
                {
                    WebResponseInstance = (ex as WebException).Response as HttpWebResponse;
                }
            }
            if (ResponseStreamEvent != null)
            {
                ResponseStreamEvent(WebResponseInstance);
            }
        }
 
 
    }
 
    public class Utility : IUtility
    {
        public string MD5Encrypt(string input)
        {
            byte[] result = Encoding.Default.GetBytes(input);    //tbPass为输入密码的文本框
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "").ToLower();
        }
 
        public long ConvertDateTimeToLong(DateTime dt)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (long)(dt - startTime).TotalSeconds;
        }
 
        public DateTime ConvertLongToDateTime(long times)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(times + "0000000");
            TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow);
        }
    }
 
}

  

代码分析 公用类库,布布扣,bubuko.com

代码分析 公用类库

标签:blog   class   code   tar   color   http   

原文地址:http://www.cnblogs.com/blueker-li/p/3708645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!