public override void Close ()
{
if (GetResponseOnClose) {
if (disposed)
return;
disposed = true;
var response = (HttpWebResponse)request.GetResponse ();
response.ReadAll ();
response.Close ();
return;
}
if (sendChunked) {
if (disposed)
return;
disposed = true;
if (!pending.WaitOne (WriteTimeout)) {
throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
}
byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
string err_msg = null;
cnc.Write (request, chunk, 0, chunk.Length, ref err_msg);
return;
}
if (isRead) {
if (!nextReadCalled) {
CheckComplete ();
// If we have not read all the contents
if (!nextReadCalled) {
nextReadCalled = true;
cnc.Close (true);
}
}
return;
} else if (!allowBuffering) {
complete_request_written = true;
if (!initRead) {
initRead = true;
WebConnection.InitRead (cnc);
}
return;
}
if (disposed || requestWritten)
return;
long length = request.ContentLength;
if (!sendChunked && length != -1 && totalWritten != length) {
IOException io = new IOException ("Cannot close the stream until all bytes are written");
nextReadCalled = true;
cnc.Close (true);
throw new WebException ("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
}
// Commented out the next line to fix xamarin bug #1512
//WriteRequest ();
disposed = true;
}