Exemplo n.º 1
0
        static void _http_onRequest(IAsyncResult hr)
        {
            _http_beginRecv();
            if (!bRun)
            {
                return;
            }
            var reqcontext = _http_listener.EndGetContext(hr);

            if (reqcontext.Request.HttpMethod.ToLower() == "post")
            {
                byte[] buf     = new byte[reqcontext.Request.ContentLength64];
                int    bufread = 0;

                AsyncCallback onPostReq = null;
                onPostReq = (phr) =>
                {
                    bufread += reqcontext.Request.InputStream.EndRead(phr);
                    if (bufread == reqcontext.Request.ContentLength64)
                    {
                        _http_OnHttpIn(reqcontext, buf);
                    }
                    else
                    {
                        reqcontext.Request.InputStream.BeginRead(buf, bufread, (int)reqcontext.Request.ContentLength64 - bufread, onPostReq, null);
                    }
                };
                reqcontext.Request.InputStream.BeginRead(buf, 0, buf.Length, onPostReq, null);
            }
            else
            {
                _http_OnHttpIn(reqcontext, null);
            }
        }
Exemplo n.º 2
0
        void onHttpIn(IAsyncResult hr)
        {
            host.BeginGetContext(onHttpIn, null);
            var content = host.EndGetContext(hr);

            try
            {
                string from = content.Request.RemoteEndPoint.ToString();
                NameValueCollection query = content.Request.QueryString;
                string c   = query["c"];
                string s   = query["s"];
                string m   = query["m"];
                string p   = query["p"];
                int    sid = 0;
                if (string.IsNullOrEmpty(s) == false)
                {
                    sid = int.Parse(s);
                }
                string returnstr = ParserRequest(from, c, sid, m, p);

                byte[] b = System.Text.Encoding.UTF8.GetBytes(returnstr);
                content.Response.OutputStream.BeginWrite(b, 0, b.Length, (hhr) =>
                {
                    content.Response.OutputStream.EndWrite(hhr);
                    content.Response.Close();
                }, null);
            }
            catch
            {
            }
        }
Exemplo n.º 3
0
 private void OnContext(IAsyncResult ar)
 {
     try
     {
         var context = _listener.EndGetContext(ar);
         var request = new WebRequestContext(context);
         Task.Factory.StartNew(() => Run(request));
     }
     catch (ObjectDisposedException e)
     {
         // This exception is expected because we don't synchronize dispose with
         // EndGetContext...
         Log.DebugFormat("Caught expected/unavaoidable exception: {0}", e);
     }
     catch (Exception e)
     {
         Log.Error("Caught unexpected exception: {0}", e);
     }
     finally
     {
         if (!_isDisposed)
         {
             Listen();
         }
     }
 }
Exemplo n.º 4
0
 private static void Listen()
 {
     http.BeginGetContext((iar) =>
     {
         System.Net.HttpListenerContext context = http.EndGetContext(iar);
         string name = "World";
         if (context.Request.QueryString.AllKeys.Contains("name"))
         {
             name = context.Request.QueryString["name"];
         }
         System.IO.Stream stream = context.Response.OutputStream;
         byte[] encodedMessage   = System.Text.Encoding.ASCII.GetBytes("Hello " + name);
         stream.Write(encodedMessage, 0, encodedMessage.Length);
         Console.WriteLine("Request from " + name);
         context.Response.Close();
         Listen();
     }, new object());
 }
Exemplo n.º 5
0
        public void ListenerCallback(IAsyncResult result)
        {
            try
            {
                System.Net.HttpListener        listener = (System.Net.HttpListener)result.AsyncState;
                System.Net.HttpListenerContext context  = listener.EndGetContext(result);

                System.Net.HttpListenerRequest  request  = context.Request;
                System.Net.HttpListenerResponse response = context.Response;

                _iprocessRequest.ProcessRequest(context);
            }
            catch (Exception ex)
            {
                ServerComms.LogError($"Http Listener falled {ex.Message}");
                _listener.BeginGetContext(new AsyncCallback(ListenerCallback), _listener);
            }
        }
Exemplo n.º 6
0
        public void WebServerCallback(IAsyncResult result)
        {
            Trace.TraceInformation("Enter.");

            try
            {
                System.Net.HttpListener listener = (System.Net.HttpListener)result.AsyncState;

                // Call EndGetContext to complete the asynchronous operation.
                System.Net.HttpListenerContext context = listener.EndGetContext(result);

                EnqueueRequest(context);
            }
            catch (Exception ex)
            {
                Trace.TraceError("Exception:" + ex.Message + Environment.NewLine + "StackTrace:" + ex.StackTrace);
            }

            Trace.TraceInformation("Current thread ID: " + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
        }
Exemplo n.º 7
0
 public void ListenerCallback(System.IAsyncResult GetContextHandle)
 {
     System.Net.HttpListenerContext context = HttpListener.EndGetContext(GetContextHandle);
     try {
         //context.Request;
         string result = ProcessSqlXmlToJson(Query);
         using (System.IO.StreamWriter writer = new System.IO.StreamWriter(context.Response.OutputStream)) {
             writer.Write(result);
             context.Response.ContentLength64 = context.Response.OutputStream.Length;
             writer.Close();
         }
     }
     catch (System.Exception E) {
         context.Response.StatusCode = 500;
         using (System.IO.StreamWriter writer = new System.IO.StreamWriter(context.Response.OutputStream)) {
             writer.Write(E.ToString());
             context.Response.ContentLength64 = context.Response.OutputStream.Length;
             writer.Close();
         }
     }
 }
Exemplo n.º 8
0
        private void HandleRequest(IAsyncResult result)
        {
            if (!_listener.IsListening)
            {
                return;
            }

            var context = _listener.EndGetContext(result);
            var url     = context.Request.Url.ToString();
            var handler = _handlers.FirstOrDefault(h => url.StartsWith(h.UrlPrefix, StringComparison.OrdinalIgnoreCase));

            if (handler != null)
            {
                HttpRequest request = new HttpRequest(context.Request.Url.ToString());
                foreach (var header in context.Request.Headers.AllKeys)
                {
                    request.Headers.Add(header, context.Request.Headers[header]);
                }

                using (var reader = new StreamReader(context.Request.InputStream, Encoding.UTF8))
                    request.PostData = reader.ReadToEnd();

                var response = handler.Handler(request);
                context.Response.StatusCode      = (int)response.Status;
                context.Response.ContentType     = response.ContentType;
                context.Response.ContentEncoding = Encoding.UTF8;

                if (!String.IsNullOrEmpty(response.Response))
                {
                    using (var writer = new StreamWriter(context.Response.OutputStream, Encoding.UTF8))
                        writer.Write(response.Response);
                }
            }
            else
            {
                context.Response.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
            }
        }