【发布时间】:2015-10-09 02:40:51
【问题描述】:
我正在使用 C# 创建一个服务器,我能够接收请求并返回 HTML,但我不确定我必须做什么才能发送图像文件以便它们显示在页面上。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace Ambar
{
class Program
{
static void Main(string[] args)
{
TcpListener listener = new TcpListener(80);
listener.Start();
while (true)
{
Console.WriteLine("waiting for a connection");
TcpClient client = listener.AcceptTcpClient();
StreamReader sr = new StreamReader(client.GetStream());
StreamWriter sw = new StreamWriter(client.GetStream());
Console.WriteLine(client.GetStream().ToString());
try
{
string request = sr.ReadLine();
Console.WriteLine(request);
string[] tokens = request.Split(' ');
string page = tokens[1];
if (page == "/")
{
page = "/default.htm";
}
StreamReader file = new StreamReader("../../web" + page);
sw.WriteLine("HTTP/1.0 200 OK\n");
string data = file.ReadLine();
while (data != null)
{
sw.WriteLine(data);
sw.Flush();
data = file.ReadLine();
}
}
catch (Exception e)
{
sw.WriteLine("HTTP/1.0 404 OK\n");
sw.WriteLine("<H1> Future Site of Ambar Remake </H!>");
sw.Flush();
}
client.Close();
}
}
}
我可以托管任何我想要的 HTML,但如果我尝试显示类似的图像
<img src="picture.gif" alt="a picture" height="42" width="42">
我不确定如何托管该图像并将其显示在那里。
【问题讨论】:
-
您是否尝试以二进制格式发送图像?
标签: c# html image server hosting