I am writing a simple c# web server and whenever it gives a status of 404 I always get net::ERR_HTTP_RESPONSE_CODE_FAILURE instead of 404. All The other statuses work fine but this one. I tried looking for spelling mistakes but everything looks correct and also it shows failed to load resource instead of get example.com 404 (Not Found) like on every other website. I am mainly wondering why the response is different when compared to other websites.
Here the example of the response that gives these results:
HTTP/1.1 404 Not Found
Date:
Server: MADSC
Content-Length: 196
Content-Type: text/html
Here the code that generates the response
public void WriteToStream(Stream stream)
{
try
{
string httpHeaders;
if (dataPath.Length > 0)
{
FileStream fs = new FileStream(dataPath, FileMode.Open, FileAccess.Read);
long streamSize = fs.Length;
httpHeaders = GenerateHttpHeaders(streamSize);
WriteHttpHeadersToStream(httpHeaders, stream);
fs.CopyTo(stream);
return;
}
int dataSize = data.Length;
httpHeaders = GenerateHttpHeaders(dataSize);
WriteHttpHeadersToStream(httpHeaders, stream);
}
catch (Exception e)
{
Console.WriteLine("Exception While Writing Response:" + e.ToString());
}
}
string GenerateHttpHeaders(long dataSize)
{
//Gets Response Code Discription
string responseCodeText = HttpResponseCodes.ResponseCodeText(responseCode);
//Creates And Adds Status Line
string httpStringData = $"HTTP/1.1 {responseCode} {responseCodeText}\n";
//If Present Adds Headers
httpStringData += HeadersToString();
httpStringData += $"Date: \nServer: {server}\n";
httpStringData += $"Content-Length: {dataSize}\nContent-Type: {contentType}\n";
//Adds Data Seperator
httpStringData += "\n";
return httpStringData;
}