public void SaveAs (string filename, bool includeHeaders)
{
Stream output = new FileStream (filename, FileMode.Create);
if (includeHeaders) {
StringBuilder sb = new StringBuilder ();
string version = String.Empty;
string path = "/";
if (worker_request != null) {
version = worker_request.GetHttpVersion ();
path = UrlComponents.Path;
}
string qs = UrlComponents.Query;
sb.AppendFormat ("{0} {1}{2} {3}\r\n", HttpMethod, path, qs, version);
NameValueCollection coll = Headers;
foreach (string k in coll.AllKeys) {
sb.Append (k);
sb.Append (':');
sb.Append (coll [k]);
sb.Append ("\r\n");
}
sb.Append ("\r\n");
// latin1
byte [] bytes = Encoding.GetEncoding (28591).GetBytes (sb.ToString ());
output.Write (bytes, 0, bytes.Length);
}
// More than 1 call to SaveAs works fine on MS, so we "copy" the stream
// to keep InputStream in its state.
Stream input = GetSubStream (InputStream);
try {
long len = input.Length;
int buf_size = (int) Math.Min ((len < 0 ? 0 : len), 8192);
byte [] data = new byte [buf_size];
int count = 0;
while (len > 0 && (count = input.Read (data, 0, buf_size)) > 0) {
output.Write (data, 0, count);
len -= count;
}
} finally {
output.Flush ();
output.Close ();
EndSubStream (input);
}
}