c#使用HttpListener监听HTTP请求

最近在项目上需要与第三方系统对接,对方会通过HTTP请求定时推送数据,因此需要在项目中添加监听HTTP请求的功能,查阅了相关资料,使用System.Net下的HttpListener实现此功能。


项目采用的是WPF,在界面上使用一个CheckBox用来控制是否监听HTTP请求,此外将项目自身绑定的IP地址和端口号写在app.config文件中。

基本变量
        private static string _listenerUri = ConfigurationManager.AppSettings["ListenerUri"];
        private HttpListener _listener;
控制监听HTTP请求
 //启用HTTP请求监听
 private void checkListen_Checked(object sender, RoutedEventArgs e)
        {
            _listener = new HttpListener();
            _listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
            _listener.Prefixes.Add(_listenerUri);
            _listener.Start();
            WriteToStatus("启用数据监听!");
            _listener.BeginGetContext(ListenerHandle, _listener);
        }

        //停止HTTP请求监听
        private void checkListen_Unchecked(object sender, RoutedEventArgs e)
        {
            if (_listener != null)
            {
                _listener.Close();
                WriteToStatus("停止数据监听");
            }
        }
处理HTTP请求数据

 private void ListenerHandle(IAsyncResult result)
        {
            try
            {
                _listener = result.AsyncState as HttpListener;
                if (_listener.IsListening)
                {
                    _listener.BeginGetContext(ListenerHandle, result);
                    HttpListenerContext context = _listener.EndGetContext(result);
                    //解析Request请求
                    HttpListenerRequest request = context.Request;
                    string content = "";
                    switch (request.HttpMethod)
                    {
                        case "POST":
                            {
                                Stream stream = context.Request.InputStream;
                                StreamReader reader = new StreamReader(stream, Encoding.UTF8);
                                content = reader.ReadToEnd();
                            }
                            break;
                        case "GET":
                            {
                                var data = request.QueryString;
                            }
                            break;
                    }
                    WriteToStatus("收到数据:" + content);

                    //构造Response响应
                    HttpListenerResponse response = context.Response;
                    response.StatusCode = 200;
                    response.ContentType = "application/json;charset=UTF-8";
                    response.ContentEncoding = Encoding.UTF8;
                    response.AppendHeader("Content-Type", "application/json;charset=UTF-8");

                    using (StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.UTF8))
                    {
                        writer.Write("");
                        writer.Close();
                        response.Close();
                    }
                }

            }
            catch (Exception ex)
            {

                WriteToStatus(ex.Message);
            }
        }

你可能感兴趣的:(ASP.NET,WPF)