我有一个用C#编写的小型HTTP服务器,直到现在我只需要将原始文本发送回发件人。但现在我必须发送一张JPG图片,我不会介意如何。C#通过HTTP发送图像
这是我现在有:
// Read the HTTP Request
Byte[] bReceive = new Byte[MAXBUFFERSIZE];
int i = socket.Receive(bReceive, bReceive.Length, 0);
//Convert Byte to String
string sBuffer = Encoding.ASCII.GetString(bReceive);
// Look for HTTP request
iStartPos = sBuffer.IndexOf("HTTP", 1);
// Extract the Command without GET_/ at the beginning and _HTTP at the end
sRequest = sBuffer.Substring(5, iStartPos - 1 - 5);
String answer = handleRequest(sRequest);
// Send the response
socket.Send(Encoding.UTF8.GetBytes(answer));
我认为我必须做某种文件流而不是字符串,但我真的没有胶水..
你能张贴您的handleRequest方法的一部分?我猜这就是你在构建HTTP响应对象的地方,这个响应对象将被发送回发出请求的浏览器。你必须弄清楚如何修改它以支持图像。 – 2010-11-11 13:43:03
如果你想借用它,我有一些胶水:/ – jlafay 2010-11-11 13:53:12
@Martin如果你正在读取文件,你可以调用socket.SendFile。看到这里(msdn.microsoft.com/en-us/library/sx0a40c2.aspx – 2010-11-11 13:58:27