Use of HttpListener

前端 未结 2 1012
孤城傲影
孤城傲影 2021-02-14 18:02

I have the following HTTP listener method, greatly inspired by MSDN\'s example use of the HttpListener class. I\'m fairly new to programming and I\'m not sure where to go from h

相关标签:
2条回答
  • 2021-02-14 18:15

    you can do something like this :

       public void ListenTraces()
        {
            httpListener.Prefixes.Add(PORT_HOST);
            try
            {
                httpListener.Start();
            }
            catch (HttpListenerException hlex)
            {
                log.Warn("Can't start the agent to listen transaction" + hlex);
                return;
            }
            log.Info("Now ready to receive traces...");
            while (true)
            {
                var context = httpListener.GetContext(); // get te context 
    
                log.Info("New trace connexion incoming");
               Console.WriteLine(context.SomethingYouWant);
            }
        }
    
    0 讨论(0)
  • 2021-02-14 18:33

    You seem to have removed the comments that are mentioned on the MSDN HttpListener Class page:

    // URI prefixes are required, for example "http://contoso.com:8080/index/".

    So just call it like that:

    public static void Main(string[] args)
    {
        HttpListener(new[] { "http://localhost/" });
    }
    

    But please note this example will handle only one request and then exit. If your follow-up question is then "How can I make it handle multiple requests?", see Handling multiple requests with C# HttpListener.

    0 讨论(0)
提交回复
热议问题