wxr
2020-03-13 171bf03f3664226eeff2b20ee9bd2e914b63a17d
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
using System;
using System.Linq;
using System.Web;
//using HDL_ON.UI;
 
namespace HDL_ON.DAL.Net
{
    public static class HttpListener
    {
        private static System.Net.HttpListener listener;
 
        /// <summary>
        /// 启动监听及处理
        /// </summary>
        public static void Start(System.Net.IPAddress ipAddress, int port)
        {
            try
            {
                if (listener == null)
                {
                    listener = new System.Net.HttpListener();
 
                    listener.AuthenticationSchemes = System.Net.AuthenticationSchemes.Anonymous;
 
                    //listener.Prefixes.Add (string.Format ("http://{0}:{1}/", ipAddress.ToString (), port));
                    listener.Prefixes.Add($"http://+:{port}/");
                    listener.Start();
 
                    //beginGetContext(listener);
                    contextThread = new System.Threading.Thread(() =>
                    {
                        while (true)
                        {
                            var context = listener.GetContext();
                            System.Threading.Tasks.Task.Run(() =>
                            {
                                manager(context);
                            });
                        }
                    })
                    { IsBackground = true };
                    contextThread.Start();
                    MainPage.Log("HttpListener已经启动!");
                }
            }
            catch (Exception e)
            {
                MainPage.Log("HttpListener启动失败!\r\n" + e.Message);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        static System.Threading.Thread contextThread;
        /// <summary>
        /// 请求事件
        /// </summary>
        /// <param name="commandType">请求命令</param>
        /// <param name="outStream">输出流</param>
        /// <param name="inStream">输入流</param>
        public delegate void RequestEventHandler(System.Collections.Specialized.NameValueCollection nameValueCollection, System.IO.Stream outputStream, System.IO.Stream inputStream);
 
        /// <summary>
        /// 请求事件
        /// </summary>
        public static event RequestEventHandler EventHandler;
 
        /// <summary>
        /// 开始异步接收http请求
        /// </summary>
        /// <param name="listerner"></param>
        private static void beginGetContext(System.Net.HttpListener listerner)
        {
            try
            {
                if (listerner == null || !listerner.IsListening)
                {
                    return;
                }
                listerner.BeginGetContext(new System.AsyncCallback(getContextCallBack), null);
 
            }
            catch
            {
                System.Threading.Thread.Sleep(1);
                beginGetContext(listerner);
            }
        }
 
        /// <summary>
        /// 获取http请求,并处理
        /// </summary>
        /// <param name="ar"></param>
        private static void getContextCallBack(System.IAsyncResult ar)
        {
            beginGetContext(listener);
            try
            {
                System.Net.HttpListenerContext context = listener.EndGetContext(ar);
                manager(context);
            }
            catch { }
        }
 
        /// <summary>
        /// 关闭
        /// </summary>
        public static void Close()
        {
            try
            {
                if (contextThread != null)
                {
                    contextThread.Abort();
                    contextThread = null;
                }
            }
            catch { }
            try
            {
                if (listener != null)
                {
                    listener.Close();
                    listener = null;
                }
            }
            catch { }
            MainPage.Log("HttpListener已经关闭!");
        }
 
        private static void manager(System.Net.HttpListenerContext httpListenerContext)
        {
            System.Net.HttpListenerResponse httpListenerResponse = null;
 
            try
            {
                #region
                httpListenerResponse = httpListenerContext.Response;
 
                httpListenerResponse.StatusCode = 200;//设置返回给客服端http状态代码
                httpListenerResponse.ContentEncoding = System.Text.Encoding.UTF8;
 
 
                if (EventHandler != null)
                {
                    EventHandler(HttpUtility.ParseQueryString(System.IO.Path.GetFileName(httpListenerContext.Request.RawUrl)), httpListenerResponse.OutputStream, httpListenerContext.Request.InputStream);
                }
                httpListenerResponse.OutputStream.Flush();
 
                #endregion
            }
            catch
            {
 
            }
            finally
            {
                #region
                try
                {
                    httpListenerContext.Request.InputStream.Close();
                }
                catch { }
 
                try
                {
                    httpListenerResponse.OutputStream.Close();
                }
                catch { }
 
 
                try
                {
                    httpListenerResponse.Close();
                }
                catch { }
 
                #endregion
            }
        }
 
        public static void PostData(string url, string postJson)
        {
            System.IO.Stream outputStream = null;
            System.IO.Stream inputStream = null;
            try
            {
                System.Net.HttpWebRequest webReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(url));
                webReq.Method = "POST";
                //webReq
 
                byte[] byteData = System.Text.Encoding.UTF8.GetBytes(postJson);
                //定义传送数据格式
                webReq.ContentType = "application/json";
                webReq.Accept = "application/json";
                //webReq.ContentType = "application/x-www-form-urlencoded";
                //webReq.Timeout = 1000*2;
                webReq.ContentLength = byteData.Length;
                //定义Stream信息
                outputStream = webReq.GetRequestStream();
                outputStream.Write(byteData, 0, byteData.Length);
                outputStream.Flush();
                outputStream.Close();
 
                //inputStream=  webReq.GetResponse().GetResponseStream();
            }
            catch
            {
                throw new Exception("发送失败");
            }
            finally
            {
                if (null != outputStream)
                {
                    outputStream.Close();
                }
                if (null != inputStream)
                {
                    inputStream.Close();
                }
            }
        }
    }
}