我有一个SimpleHttp服务器的以下代码:
using (Stream fs = File.Open(@"C:\Users\Mohamed\Desktop\Hany.jpg", FileMode.Open))
{
StreamWriter OutputStream = new StreamWriter(new BufferedStream(someSocket.GetStream()));
OutputStream.WriteLine("HTTP/1.0 200 OK");
OutputStream.WriteLine("Content-Type: application/octet-stream");
OutputStream.WriteLine("Content-Disposition: attachment; filename=Hany.jpg");
OutputStream.WriteLine("Content-Length: " + img.Length);
OutputStream.WriteLine("Connection: close");
OutputStream.WriteLine(""); // this terminates the HTTP headers
fs.CopyTo(OutputStream.BaseStream);
OutputStream.Flush();
//OutputStream.BaseStream.Flush();
}
问题是,当我看到输出http响应时,标题位于文本末尾,而BaseStream中的图像二进制文件首先出现在标题之前。输出样本(当然我删除了图像的长字节):
ä3ST)ëî!ðDFYLQ>qâ:oÂÀó?ÿÙHTTP/1.0 200 OK
Content-Type: image/png
Connection: close
我想要的是颠倒顺序并将标题放在最上面,得到的是这样的:
HTTP/1.0 200 OK
Content-Type: image/png
Connection: close
ä3ST)ëî!ðDFYLQ>qâ:oÂÀó?ÿÙT4ñ®KÛ'`ÃGKs\CGÔ«¾+L»ê±?0Íse3rïÁå·>"ܼ;®N¦Ãõ5¨LZµL¯
在流编写器或BaseStream上使用flush并不重要。 任何帮助!
答案 0 :(得分:0)
我认为问题是由调用CopyTo和传递BaseStream引起的。它可能会绕过尚未刷新数据的StreamWriter。
不应使用BaseStream进行写入。必须使用StreamWriter。
using (StreamWriter outputStream = new StreamWriter(new BufferedStream(someSocket.GetStream())))
{
outputStream.WriteLine("HTTP/1.0 200 OK");
outputStream.WriteLine("Content-Type: application/octet-stream");
outputStream.WriteLine("Content-Disposition: attachment; filename=Hany.jpg");
outputStream.WriteLine("Content-Length: " + img.Length);
outputStream.WriteLine("Connection: close");
outputStream.WriteLine(""); // this terminates the HTTP headers
string imageContent = Convert.ToBase64String(File.ReadAllBytes(@"C:\Users\Mohamed\Desktop\Hany.jpg"));
outputStream.Write(imageContent);
outputStream.Flush();
}
答案 1 :(得分:0)
谢谢Kzrystof,我接受了你的提示,它现在可以在使用copyTo之前刷新StreamWriter,但我真的不知道这是否正确?你觉得怎么样?
colors = {2013: 'r', 2014: 'b', 2015: 'g'}
fig, ax = plt.subplots(figsize=(10,10))
labels = []
for key, grp in dqData[dqData['team'] == 'team1'].groupby(['year']):
g2 = grp.pivot(index='x', columns='month', values='y')
ax = g2.plot(ax=ax, kind='line', c = colors[key])
labels.append(key)
lines, _ = ax.get_legend_handles_labels()
ax.legend(lines, labels, loc='best')
plt.show()