System.Net.WebConnectionStream.Close C# (CSharp) Method

Close() public method

public Close ( ) : void
return void
		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;
		}

Usage Example

Esempio n. 1
0
		public override void Abort ()
		{
			if (Interlocked.CompareExchange (ref aborted, 1, 0) == 1)
				return;

			if (haveResponse && finished_reading)
				return;

			haveResponse = true;
			if (abortHandler != null) {
				try {
					abortHandler (this, EventArgs.Empty);
				} catch (Exception) {}
				abortHandler = null;
			}

			if (asyncWrite != null) {
				WebAsyncResult r = asyncWrite;
				if (!r.IsCompleted) {
					try {
						WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled); 
						r.SetCompleted (false, wexc);
						r.DoCallback ();
					} catch {}
				}
				asyncWrite = null;
			}			

			if (asyncRead != null) {
				WebAsyncResult r = asyncRead;
				if (!r.IsCompleted) {
					try {
						WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled); 
						r.SetCompleted (false, wexc);
						r.DoCallback ();
					} catch {}
				}
				asyncRead = null;
			}			

			if (writeStream != null) {
				try {
					writeStream.Close ();
					writeStream = null;
				} catch {}
			}

			if (webResponse != null) {
				try {
					webResponse.Close ();
					webResponse = null;
				} catch {}
			}
		}		
All Usage Examples Of System.Net.WebConnectionStream::Close