I was testing C# HttpListener with VS2019, console application for .net core 3.1
First, I used powershell under admin mode and ran:
> netsh http add urlacl url=http://+:80/ user=everyone
Then my C# program as below:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("...Wait until all async finish");
HttpListener listener = new HttpListener();
listener.Prefixes.Add(@"http://+:80/");
listener.Start();
while (true)
{
Console.WriteLine("Wait one connection");
var context = listener.GetContext();
var response = context.Response;
Console.WriteLine("... Got one connection response");
var responseString = $"<html><body><h1>Hellow,How are You?{DateTime.Now}</h1></body></html>";
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
using (var stream = response.OutputStream)
{
stream.Write(buffer, 0, buffer.Length);
}
Console.WriteLine("Response done\n");
}
}
}
Problem is, each time I use edge or chrome to browse "localhost", the while loop run 2 times, and print the line "Response done" two times.
I expect that each time I create a new browser tag for "localhost" or refrsh the page, my program should only enter while loop once. So what's the problem like?
Thanks a lot.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.