我见过的最接近的是SaveAs方法,它保存到磁盘 - 这不是一个很好的选择。我确实查看了该代码,它几乎可以满足您的期望。也许基于SaveAs代码的HttpRequestBase上的扩展方法可以吗?
为了省点麻烦,下面是框架的SaveAs实现:
public void SaveAs(string filename, bool includeHeaders)
{
if (!Path.IsPathRooted(filename) && RuntimeConfig.GetConfig(this._context).HttpRuntime.RequireRootedSaveAsPath)
{
object[] objArray = new object[] { filename };
throw new HttpException(SR.GetString("SaveAs_requires_rooted_path", objArray));
}
FileStream fileStream = new FileStream(filename, FileMode.Create);
try
{
if (includeHeaders)
{
TextWriter streamWriter = new StreamWriter(fileStream);
streamWriter.Write(string.Concat(this.HttpMethod, " ", this.Path));
string queryStringText = this.QueryStringText;
if (!string.IsNullOrEmpty(queryStringText))
{
streamWriter.Write(string.Concat("?", queryStringText));
}
if (this._wr == null)
{
streamWriter.Write("\r\n");
}
else
{
streamWriter.Write(string.Concat(" ", this._wr.GetHttpVersion(), "\r\n"));
streamWriter.Write(this.CombineAllHeaders(true));
}
streamWriter.Write("\r\n");
streamWriter.Flush();
}
((HttpInputStream)this.InputStream).WriteTo(fileStream);
fileStream.Flush();
}
finally
{
fileStream.Close();
}
}