Hi me again! =)
if you didn't already know i am making a simple HTTP Server using TCP/IP sockets for a project.
i have a method to get an image and send it over to the browser but i am having problems doing so, i have tried doing it different ways but nothing seems to be working.
the browser asks for the image, the server sends the head along with the image but the browser never shows the image. the server works fine for all the html/css files i have send to the browser.
Please help me!!!
oh and yess i am using a browser that accepts uses favicon.ico. =)
Here is some of my code:
public void run()
{
mainStream = new NetworkStream(connection);
inStream = new BinaryReader(mainStream);
outStream = new BinaryWriter(mainStream);
byte[] item = null;
//or
string item = null;
//depending on which method is called
string head = null;
string lastModified = null;
if (requestItem == "/favicon.ico")
{
lastModified = String.Format("{0:ddd, dd MMM yy HH:mm:ss}", File.GetLastWriteTime("Resources/favicon.ico"));
//calls method below
item = readItem("Resources/favicon.ico", "image/png");
head += "HTTP/1.1 206 Partial Content\r\n";
head += String.Format("Date: {0:ddd, dd MMM yy HH:mm:ss} GMT\r\n", DateTime.Now);
head += "Server: Ollie's Server v1.0\r\n";
head += String.Format("Last-Modified: {0} GMT\r\n", lastModified);
head += String.Format("ETag: \"{0}\"\r\n", MD5Hashing(requestItem + lastModified));
head += "Content-Type: image/gif\r\n";
head += string.Format("Content-Length: {0}\r\n", item.Length);
head += "\r\n";
if (requestType == "GET")
{
outStream.Write(head);
outStream.Write(item, 0, item.Length);
//or
outStream.Write(item);
//depending on which method is called
}
else if (requestType == "HEAD")
{
outStream.Write(head);
}
}
}
private byte[] readItem(string file)
{
byte[] output = null;
try
{
MemoryStream ms = new MemoryStream();
System.Drawing.Image i = System.Drawing.Image.FromFile(file);
i.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
i.Dispose();
ms.Flush();
ms.Close();
output = ms.ToArray();
}
catch (Exception exp)
{
errorMessage(exp);
}
return output;
}
OR this method, both don't work! :/
private string readItem(string file)
{
string output = null;
byte[] temp = null;
try
{
MemoryStream ms = new MemoryStream();
System.Drawing.Image i = System.Drawing.Image.FromFile(file);
i.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
i.Dispose();
ms.Flush();
ms.Close();
temp = ms.ToArray();
for(int a=0; a<temp.Length; a++)
{
output += temp[a];
}
}
catch (Exception exp)
{
//method that deals with the exp
errorMessage(exp);
}
}
return output;
}
Thanks in advance =)
Ollie