title: CORS access for HttpListener
body: <p>I have a C# console app, running an <code>HttpListener</code>, and my clients are getting denied because of CORS.</p>
  
  <p>How do I set <code>Access-Allow-All-Origins</code> to <code>*</code> with my setup?</p>
  
  <pre><code>listener = new HttpListener();
  listener.Prefixes.Add("http://+:80/");
  listener.Start();
  
      public static void ListenerCallback(IAsyncResult result)
      {
          HttpListenerContext context = Program.listener.EndGetContext(result);
          HttpListenerRequest request = context.Request;
          HttpListenerResponse response = context.Response;
          if (request.HttpMethod == "OPTIONS")
          {
              response.AddHeader("Access-Control-Allow-Headers", "*");
          }
          response.AppendHeader("Access-Control-Allow-Origin", "*");
          System.IO.Stream stream = new System.IO.MemoryStream();
          request.InputStream.CopyTo(stream);
          stream.Position = 0;
          NameValueCollection coll = request.QueryString;
  
          if (String.IsNullOrEmpty(coll["name"]) || String.IsNullOrEmpty(coll["ext"]))
          {
              response.StatusCode = 400;
              response.ContentType = "text/html";
              using (StreamWriter writer = new StreamWriter(context.Response.OutputStream, Encoding.UTF8))
                  writer.WriteLine("Missing parameters in queryString. Send 'name' and 'ext'");
              response.Close();
          }
          else
          {
              Program.nameResDictionary.Add(coll["name"] + "." + coll["ext"], response);
              using (var outp = File.OpenWrite(Path.Combine(Program.inDir,coll["name"] + "." + coll["ext"])))
              {
                  stream.CopyTo(outp);
              }
  
              toLog.Add("File " + coll["name"] + "." + coll["ext"] + " added");
          }
          stream.Close();
          request.InputStream.Close();
          listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
      }
  </code></pre>