- using AndroidPush.Common;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- namespace AndroidPush.WindowsService
- {
- public class HttpService
- {
- private HttpListener listeren = new HttpListener();
- /// <summary>
- /// 初始化
- /// </summary>
- public void Init()
- {
- try
- {
- //指定身份验证 Anonymous匿名访问
- listeren.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
- //创建IP地址
- IPAddress address = IPAddress.Parse(Config._ip);
- listeren.Prefixes.Add("http://" + address + ":" + Config._porthttp + "/");
- listeren.Start();
- Thread threadlistener = new Thread(new ThreadStart(ThreadStartListener));
- threadlistener.IsBackground = true;
- threadlistener.Start();
- LogHelper.WriteLog("HTTP success ip:" + Config._ip + "_port:" + Config._porthttp);
- }
- catch (Exception ex)
- {
- LogHelper.WriteLog(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + "." + this.GetType().Name + "." + System.Reflection.MethodInfo.GetCurrentMethod().Name, ex);
- }
- }
- /// <summary>
- /// 监听连接线程
- /// </summary>
- private void ThreadStartListener()
- {
- while (true)
- {
- //int i = 0;
- // 注意: GetContext 方法将阻塞线程,直到请求到达
- HttpListenerContext context = listeren.GetContext();
- //接收到请求,用线程去处理,防止阻塞
- Thread subThread = new Thread(new ParameterizedThreadStart((currContext) =>
- {
- var request = (HttpListenerContext)currContext;
- try
- {
- //post请求
- if (request.Request.HttpMethod.ToLower().Equals("post"))
- {
- string json = PostInput(request.Request);
- Writer("success", request);
- }
- else {
- Writer("error", request);
- }
- }
- catch (Exception ex)
- {
- LogHelper.WriteLog(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + "." + this.GetType().Name + "." + System.Reflection.MethodInfo.GetCurrentMethod().Name, ex);
- Writer("error", request);
- }
- }));
- subThread.Start(context);
- }
- }
- /// <summary>
- /// HttpListener接收post请求
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- private string PostInput(HttpListenerRequest request)
- {
- try
- {
- System.IO.Stream s = request.InputStream;
- int count = 0;
- byte[] buffer = new byte[1024];
- StringBuilder builder = new StringBuilder();
- while ((count = s.Read(buffer, 0, 1024)) > 0)
- {
- builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
- }
- s.Flush();
- s.Close();
- s.Dispose();
- return builder.ToString();
- }
- catch {
- return null;
- }
- }
- /// <summary>
- /// 响应内容
- /// </summary>
- /// <param name="str"></param>
- /// <param name="context"></param>
- public void Writer(string str, HttpListenerContext context)
- {
- HttpListenerRequest request = context.Request;
- HttpListenerResponse response = context.Response;
- // 构造回应内容
- string responseString = str;
- // 设置回应头部内容,长度,编码
- response.ContentLength64
- = System.Text.Encoding.UTF8.GetByteCount(responseString);
- response.ContentType = "text/html; charset=UTF-8";
- // 输出回应内容
- System.IO.Stream output = response.OutputStream;
- System.IO.StreamWriter writer = new System.IO.StreamWriter(output);
- writer.Write(responseString);
- // 必须关闭输出流
- writer.Close();
- }
- }
- }
复制代码
|