次の方法で共有


Using HTTP in System.Net

I think that the HttpWebRequest class for making HTTP requests in System.Net is fairly well understood but not that many people seem to make use of the HttpListener class for receiving requests. I use HttpListener when I need to see what WCF is doing under the covers when making a web request. By eliminating WCF from the server, I can play with the requests without any of the processing that might secretly go on.

Today's article is an example of one of those test programs. I wrote a simple listener service that just prints out information about the request for inspection. I would use something like this for testing a WCF client that fiddles with how the request is structured. Since I don't know what the request is for, I just say that the request is accepted for future processing without giving an immediate response. This type of test program is for a one-way contract that doesn't expect any return data.

 using System;
using System.IO;
using System.Net;
using System.Threading;

class HttpTestListener
{
   static bool shutdown = false;

   static void Process(object state)
   {
      HttpListener listener = (HttpListener)state;
      listener.Start();
      while (listener.IsListening)
      {
         try
         {
            HttpListenerContext context = listener.GetContext();
            HttpListenerRequest request = context.Request;
            Console.WriteLine("HTTP/{0} {1} {2}", request.ProtocolVersion, request.HttpMethod, request.Url);
            Console.WriteLine("{0} --> {1}", request.RemoteEndPoint, request.LocalEndPoint);
            Console.WriteLine();
            Console.WriteLine("[Headers]");
            Console.WriteLine(request.Headers);
            if (request.HasEntityBody)
            {
               Console.WriteLine();
               Console.WriteLine("[Request]");
               StreamReader reader = new StreamReader(request.InputStream, request.ContentEncoding);
               Console.WriteLine(reader.ReadToEnd());
            }
            HttpListenerResponse response = context.Response;
            response.StatusCode = (int)HttpStatusCode.Accepted;
            response.ContentLength64 = 0;
            response.Close();
         }
         catch (HttpListenerException exception)
         {
            if (!shutdown)
            {
               Console.WriteLine(exception);
            }
         }
      }
   }

   static void Main(string[] args)
   {
      HttpListener listener = new HttpListener();
      foreach (string arg in args)
      {
         listener.Prefixes.Add(arg);
      }
      listener.Prefixes.Add("https://localhost:8000/");
      new Thread(new ParameterizedThreadStart(Process)).Start(listener);
      Console.WriteLine("Press <ENTER> to quit.");
      Console.ReadLine();
      Console.WriteLine("Shutting down...");
      shutdown = true;
      listener.Close();
      Thread.Sleep(1000);
      Environment.Exit(0);
   }
}

As you might expect, we now need to play with this new toy. The next few days are going to be spent investigating what's going on during one-way HTTP requests.

Next time: Making One-Way HTTP Requests, Part 1

Comments