新框架PDA前端(祈禧6月初版本)
南骏 池
3 天以前 3ff569f00d22919e47cab8fb6d7d9867fd115d97
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
using System;
using System.Web;
using System.Text;
using System.Net;
using System.IO;
 
namespace WebApp
{
    public class Utility
    {
        /// <summary>
        /// 读取BasicAuth Ticket
        /// </summary>
        /// <returns></returns>
        public static string GetBasicAuthTicket()
        {
            cookidLoginUser m = new cookidLoginUser();
            m = GetLoginUser();
            if (m != null)
            {
                string cookieGuid = m.loginGuid;//登录者guid
                return "BasicAuth " + GetTimestamp().ToString() + "_" + cookieGuid;
            }
            else
                return "BasicAuth " + GetTimestamp().ToString() + "_" + "null";
 
        }
        /// <summary>
        /// 读取登录实体
        /// </summary>
        /// <returns></returns>
        public static cookidLoginUser GetLoginUser()
        {
            HttpCookie cookies = HttpContext.Current.Request.Cookies["bthUserInfo"];//取cookie
            if (cookies == null)
                return null;
            cookidLoginUser m = new cookidLoginUser();
            m.loginGuid = HttpUtility.UrlDecode(cookies.Values["loginGuid".ToUpper()]);//这是用户的guid
            m.loginAgtGuid = HttpUtility.UrlDecode(cookies.Values["loginAgtGuid".ToUpper()]);
            m.loginRight = HttpUtility.UrlDecode(cookies.Values["loginRight".ToUpper()]);
            m.loginRightList = HttpUtility.UrlDecode(cookies.Values["loginRightList".ToUpper()]);
            return m;
        }
        
        /// <summary>
        /// http get 读取数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string HttpGet(string url)
        {
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/json";
            request.Headers.Add("Authorization", GetBasicAuthTicket());
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="param"> @"{ ""mmmm"": ""89e"",""nnnnnn"": ""0101943"",""kkkkkkk"": ""e8sodijf9""}"</param>
        /// <returns></returns>
        public static string HttpPost(string url, string param)
        {
            string serviceAddress = url;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.Headers.Add("Authorization", GetBasicAuthTicket());
            string strContent = param;
            using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
            {
                dataStream.Write(strContent);
                dataStream.Close();
            }
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码  
                }
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                string retString = reader.ReadToEnd();
                return retString;
            }
            catch (System.Net.WebException ex)
            {
                throw ex;
            }
 
 
        }
 
 
        /// <summary>
        /// 读取时间戳
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static double GetTimestamp()
        {
            DateTime d = DateTime.Now;
            TimeSpan ts = d.ToUniversalTime() - new DateTime(1970, 1, 1);
            return ts.TotalMilliseconds;     //精确到毫秒
        }
 
        /**
       * 向日志文件写入出错信息
       * @param className 类名
       * @param content 写入内容
       */
        public static void Error(string className, string content)
        {
            WriteLog("ERROR", className, content);
        }
 
        /**
        * 实际的写日志操作
        * @param type 日志记录类型
        * @param className 类名
        * @param content 写入内容
        */
        private static void WriteLog(string type, string className, string content)
        {
            string path = HttpContext.Current.Server.MapPath("~") + "logs";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
 
            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间
            string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名
 
            //创建或打开日志文件,向日志文件末尾追加记录
            StreamWriter mySw = File.AppendText(filename);
 
            //向日志文件写入内容
            string write_content = time + " " + type + " " + className + ": " + content;
            mySw.WriteLine(write_content);
 
            //关闭日志文件
            mySw.Close();
        }
    }
 
    public class cookidLoginUser
    {
 
        /// <summary>
        /// 登录者guid
        /// </summary>
        public string loginGuid
        {
            set;
            get;
        }
 
        /// <summary>
        /// 登录者agtGuid
        /// </summary>
        public string loginAgtGuid
        {
            set;
            get;
        }
        /// <summary>
        /// 登录者rightloginRightList
        /// </summary>
        public string loginRight
        {
            set;
            get;
        }
        /// <summary>
        /// 登录者loginRightList
        /// </summary>
        public string loginRightList
        {
            set;
            get;
        }
    }
}