C#简单Web服务器映像读入
所以我回来问另一个关于我的web服务器的问题。自从上一个问题以来,我已经为它添加了一些新功能,但现在我遇到了另一个问题 我试图允许服务器提供通过HTML调用的图像。但是,由于某些原因,该图像没有显示在网页上。我已经浏览了代码,它看起来确实在通过文件发送,但它只是没有显示在另一端。这是我的密码:C#简单Web服务器映像读入,c#,webserver,jpeg,httplistener,httplistenerrequest,C#,Webserver,Jpeg,Httplistener,Httplistenerrequest,所以我回来问另一个关于我的web服务器的问题。自从上一个问题以来,我已经为它添加了一些新功能,但现在我遇到了另一个问题 我试图允许服务器提供通过HTML调用的图像。但是,由于某些原因,该图像没有显示在网页上。我已经浏览了代码,它看起来确实在通过文件发送,但它只是没有显示在另一端。这是我的密码: using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
namespace miniWebServer_csc360
{
public partial class Form1 : Form
{
public static int requestCount = 0;
public static Boolean singleUse = true;
public static HttpListener listener = new HttpListener();
//public static HttpListenerContext context;
//public static HttpListenerResponse response;
static Form1 thisform;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
thisform = this;
}
private void start_button_Click(object sender, EventArgs e)
{
Thread t = new Thread(serverThread);
t.Start();
}
public static void serverThread()
{
if (singleUse)
{
listener.Prefixes.Add("http://10.211.55.4:69/");
listener.Prefixes.Add("http://localhost:69/");
singleUse = false;
}
if (listener.IsListening)
listener.Stop();
else
listener.Start();
if (listener.IsListening)
{
thisform.Invoke((MethodInvoker)delegate { thisform.status_label.Text
= "Listening..."; });
thisform.Invoke((MethodInvoker)delegate { thisform.start_button.Text
= "Stop Server"; });
}
else
{
thisform.Invoke((MethodInvoker)delegate { thisform.status_label.Text
= "Not listening..."; });
thisform.Invoke((MethodInvoker)delegate { thisform.start_button.Text
= "Start Server"; });
}
while (listener.IsListening)
{
try
{
HttpListenerContext context = listener.GetContext();
HttpListenerResponse response = context.Response;
requestCount++;
thisform.Invoke((MethodInvoker)delegate { thisform.counter.Text
= requestCount.ToString(); });
string page = Directory.GetCurrentDirectory()
+ context.Request.Url.LocalPath;
if (context.Request.Url.LocalPath != "/HEAD.html"
&& context.Request.Url.LocalPath != "/favicon.ico")
{
if (context.Request.Url.LocalPath == "/")
{
string path = context.Request.Url.LocalPath + "index.html";
thisform.Invoke((MethodInvoker)delegate
{
thisform.request_Box.AppendText(path + "\r\n");
});
}
else
{
thisform.Invoke((MethodInvoker)delegate
{
thisform.request_Box.AppendText
(context.Request.Url.LocalPath + "\r\n");
});
}
}
if (context.Request.Url.LocalPath == "/")
page += "index.html";
TextReader tr = new StreamReader(page);
string msg = tr.ReadToEnd();
byte[] buffer = Encoding.UTF8.GetBytes(msg);
if (context.Request.Url.LocalPath == "/header.html")
{
File.WriteAllText(Directory.GetCurrentDirectory() + "/HEAD.html",
context.Request.HttpMethod + " " +
context.Request.RawUrl + " HTTP/"
+ context.Request.ProtocolVersion + "\n"
+ context.Request.Headers.ToString());
}
response.ContentLength64 = buffer.Length;
Stream st = response.OutputStream;
st.Write(buffer, 0, buffer.Length);
context.Response.Close();
}
catch (Exception error)
{
}
}
}
}
}
提前谢谢 必须将HttpListenerResponse.ContentType设置为图像的适当MIME类型,例如“image/gif”
此外,示例代码是通过文本读取器读取图像并将其转换为UTF8字节数组。这对二进制文件不起作用;它会破坏字节。您需要使用FileStream或任何其他方法读取这些文件,这些方法将返回未经修改的二进制文件内容。您能将问题缩小一点吗?此服务器的实际响应是什么?当它为图像服务时,它的标题和内容是什么?是否在
catch
块中捕获并完全忽略了错误?如何准确获取内容类型?内容类型取决于文件格式。您通常可以从文件扩展名推断它。只需尝试处理您现在拥有的一个案例,查看扩展,当您看到该扩展时,为该扩展返回适当的ContentType(硬编码)。一旦成功了,你的问题就变成了如何获得mime类型的扩展名列表,这已经有了其他很好的答案。我尝试过这样做,但它仍然没有显示在另一面。嗨,克里斯-我刚刚注意到你正在用文本阅读器读取所有文件。这对图像不起作用,因为它将修改中的字节并导致无效图像。您需要使用不会修改字节的内容读取二进制文件,例如文件流。如果您不打算将文本文件从UTF-8转换为其他编码,那么您也可以使用二进制文件读取器读取文本文件;首先确保文本文件保存为UTF-8(我们在服务器中这样做)。这很有意义,我正在查看FileStream的文档,但我似乎不知道如何在代码中实现它。你能给我指一下正确的方向吗?