Context
There is a simple self-described HTTP server that listens incoming connections. When I put ip_address:port
in browser, I should get a simple HTTP page with text information. It works fine if server accepts incoming stream from clients, parse it and read headers.
But I need to realize the same without reading any incoming streams. So I hide 3 lines of code:
//inputStream = new BufferedStream(socket.GetStream());
//parseRequest();
//readHeaders();
Then I check the result with command:
telnet address port
... and it works OK, server returns me the same text.
But in browser I get an error:
This site can’t be reached
... and in the browser`s command line there appears an ERROR:
GET http://adress:port net::ERR_CONNECTION_RESET
Question
How can I get response correctly, if I can not read HTTP request from client?
Code
public void listen()
{
listener = new TcpListener(adr, port);
listener.Start();
while (is_active)
{
TcpClient s = listener.AcceptTcpClient();
HttpProcessor processor = new HttpProcessor(s, this);
Thread thread = new Thread(new ThreadStart(processor.process));
thread.Start();
Thread.Sleep(1);
}
}
public void process()
{
outputStream = new StreamWriter(new BufferedStream(socket.GetStream()));
try {
//inputStream = new BufferedStream(socket.GetStream());
//parseRequest();
//readHeaders();
handleGETRequest();
}
catch (Exception e)
{
writeFailure();
Library.WriteLog("Exception " + e.ToString());
}
outputStream.Flush();
outputStream = null;
socket.Close();
}
public void handleGETRequest()
{
srv.handleGETRequest(this);
}
public override void handleGETRequest(HttpProcessor p)
{
p.outputStream.WriteLine("HTTP / 1.1 200 OK");
p.outputStream.WriteLine("Content-Type: text/html; charset=UTF-8");
p.outputStream.WriteLine("Transfer-Encoding: chunked");
p.outputStream.WriteLine("Connection: keep-alive");
p.outputStream.WriteLine("Keep-Alive: timeout=2000");
p.outputStream.WriteLine("Cache-Control: max-age=0\n");
p.outputStream.WriteLine("<html>");
p.outputStream.WriteLine("<head><title> 200 Test </title></head>");
p.outputStream.WriteLine("<center><h1> 302 Test2 ");
p.outputStream.WriteLine(Service1.GetData());
p.outputStream.WriteLine("</h1></center>");
p.outputStream.WriteLine("<hr><center> nginx </center>");
p.outputStream.WriteLine("</body>");
p.outputStream.WriteLine("</html>");
}