加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#中的超小型Web服务器

发布时间:2020-12-15 23:59:20 所属栏目:百科 来源:网络整理
导读:我正在编写一个用于教育目的的超小型Web服务器. 对于以下代码,如果我请求包含图像的html页面,我无法在浏览器中看到该图像.我究竟做错了什么? static void Main(string[] args){ TcpListener listener = new TcpListener(9999); listener.Start(); while (tr
我正在编写一个用于教育目的的超小型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) + "rnrn");
    output.Write(statusLine,statusLine.Length);
    byte[] ContentType = 
        (new System.Text.ASCIIEncoding()).GetBytes(GetContentType(file) + 
            "rnrn");
    output.Write(ContentType,ContentType.Length);
    byte[] response = System.IO.File.ReadAllBytes("C:" + file);
    output.Write(response,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,bufSize);
    string request = System.Text.Encoding.ASCII.GetString(bytesFrom);
    return request;
}

编辑:

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,headers.Length);
        output.Write(fileContent,fileContent.Length);
        output.Flush();
    }

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

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

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

解决方法

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

就像是

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

RAWJPEGDATA

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读