c#中的超小型Web服务器

Raj*_*aja 3 c# http http-headers

我正在编写一个用于教育目的的超小型Web服务器.

对于以下代码,如果我请求包含图像的html页面,我无法在浏览器中看到该图像.我究竟做错了什么?

static void Main(string[] args)
{
    TcpListener listener = new TcpListener(9999);
    listener.Start();
    while (true)
    { 
        TcpClient client = listener.AcceptTcpClient();
        string request = GetRequest(client.GetStream(),
            client.ReceiveBufferSize);
        WriteOutput(request, client.GetStream());
        client.Close();
    }
}

static void WriteOutput(string request, NetworkStream output)
{
    try
    {
        string[] reqs = request.Split(' ');
        WriteOutputHelper(output, reqs[1].Substring(1));
    }
    catch (Exception)
    {
        WriteOutputHelper(output, "404.html");
    }
}

private static void WriteOutputHelper(NetworkStream output, string file)
{
    byte[] statusLine = (new System.Text.ASCIIEncoding()).
        GetBytes(GetStatusLine(file) + "\r\n\r\n");
    output.Write(statusLine, 0, statusLine.Length);
    byte[] ContentType = 
        (new System.Text.ASCIIEncoding()).GetBytes(GetContentType(file) + 
            "\r\n\r\n");
    output.Write(ContentType, 0, ContentType.Length);
    byte[] response = System.IO.File.ReadAllBytes("C:\\" + file);
    output.Write(response, 0, response.Length);
    output.Flush();
}

static string GetContentType(string fileName)
{  
    string i = "<META http-equiv=\"Content-Type\" content=\"";
    if ((fileName.IndexOf(".htm") > -1) || (fileName.IndexOf(".html") > -1))
        i = i + "text/html";
    else if (fileName.IndexOf(".jpg") > -1)
        i = i + "image/jpeg";
    i = i + ";\">";
    return i;
}

static string GetStatusLine(string fileName)
{
    string i = "HTTP/1.0 ";
    if (fileName.IndexOf("404") > -1)
        return i + "404 Not Found";
    else if (fileName.IndexOf("jpg") > -1)
        return i + "302 Found";
    return i + "200 OK";
}

static string GetRequest(NetworkStream reqStream,int bufSize)
{
    byte[] bytesFrom = new byte[10025];
    reqStream.Read(bytesFrom, 0, bufSize);
    string request = System.Text.Encoding.ASCII.GetString(bytesFrom);
    return request;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

    static void imageTest(NetworkStream output)
    {
        byte[] fileContent = System.IO.File.ReadAllBytes("C:\\sachin.jpg");
        string statusLine = "HTTP/1.0 200 OK" + System.Environment.NewLine;
        string contentType = "Content-type: image/jpeg" + System.Environment.NewLine;
        string contentLength = "Content-length: " + fileContent.Length + System.Environment.NewLine;
        System.Text.UnicodeEncoding coding = new UnicodeEncoding();
        byte[] headers = coding.GetBytes(statusLine + contentType + contentLength);
        output.Write(headers, 0, headers.Length);
        output.Write(fileContent, 0, fileContent.Length);
        output.Flush();
    }
Run Code Online (Sandbox Code Playgroud)

对于上面的代码,我在fiddler中收到此错误.

服务器未返回格式正确的HTTP标头.应使用CRLFCRLF终止HTTP标头.这些都是以LFLF终止的.

我使用Unicode编码,因为我想将字符串转换为字节,我只知道使用编码.

Lou*_*nco 5

JPG的响应必须只是HTTP标头,然后是JPEG的内容,而不是它周围的任何HTML.

就像是

HTTP/1.0 200 OK
Content-type: image/jpeg
Content-length: XXXXX

RAWJPEGDATA
Run Code Online (Sandbox Code Playgroud)

使用Jpeg中的字节数填写XXXXX,直接输出原始JPEG数据,无需任何编码.

使用Fiddler或Firebug来帮助调试 - 它们显示发送的确切请求/响应.


Sve*_*ven 5

看起来您正在发送302 Foundjpeg文件的状态,这是用于重定向.您需要200 OK像对待HTML文件一样发送.