Пример #1
0
		void CheckSendError (WebConnectionData data)
		{
			// Got here, but no one called GetResponse
			int status = data.StatusCode;
			if (status < 400 || status == 401 || status == 407)
				return;

			if (writeStream != null && asyncRead == null && !writeStream.CompleteRequestWritten) {
				// The request has not been completely sent and we got here!
				// We should probably just close and cause an error in any case,
				saved_exc = new WebException (data.StatusDescription, null, WebExceptionStatus.ProtocolError, webResponse); 
				if (allowBuffering || sendChunked || writeStream.totalWritten >= contentLength) {
					webResponse.ReadAll ();
				} else {
					writeStream.IgnoreIOErrors = true;
				}
			}
		}
Пример #2
0
        void CheckSendError(WebConnectionData data)
        {
            // Got here, but no one called GetResponse
            int status = data.StatusCode;

            if (status < 400 || status == 401 || status == 407)
            {
                return;
            }

            if (writeStream != null && asyncRead == null && !writeStream.CompleteRequestWritten)
            {
                // The request has not been completely sent and we got here!
                // We should probably just close and cause an error in any case,
                saved_exc = new WebException(data.StatusDescription, null, WebExceptionStatus.ProtocolError, webResponse);
                webResponse.ReadAll();
            }
        }
Пример #3
0
		// Returns true if redirected
		bool CheckFinalStatus (WebAsyncResult result)
		{
			if (result.GotException) {
				bodyBuffer = null;
				throw result.Exception;
			}

			Exception throwMe = result.Exception;

			HttpWebResponse resp = result.Response;
			WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
			HttpStatusCode code = 0;
			if (throwMe == null && webResponse != null) {
				code = webResponse.StatusCode;
				if (!authCompleted && ((code == HttpStatusCode.Unauthorized && credentials != null) ||
				     (ProxyQuery && code == HttpStatusCode.ProxyAuthenticationRequired))) {
					if (!usedPreAuth && CheckAuthorization (webResponse, code)) {
						// Keep the written body, so it can be rewritten in the retry
						if (InternalAllowBuffering) {
							// NTLM: This is to avoid sending data in the 'challenge' request
							// We save it in the first request (first 401), don't send anything
							// in the challenge request and send it in the response request along
							// with the buffers kept form the first request.
							if (ntlm_auth_state != NtlmAuthState.Response) {
								bodyBuffer = writeStream.WriteBuffer;
								bodyBufferLength = writeStream.WriteBufferLength;
							}
							return true;
						} else if (method != "PUT" && method != "POST") {
							bodyBuffer = null;
							return true;
						}

						if (!ThrowOnError)
							return false;
							
						writeStream.InternalClose ();
						writeStream = null;
						webResponse.Close ();
						webResponse = null;
						bodyBuffer = null;
							
						throw new WebException ("This request requires buffering " +
									"of data for authentication or " +
									"redirection to be sucessful.");
					}
				}

				bodyBuffer = null;
				if ((int) code >= 400) {
					string err = String.Format ("The remote server returned an error: ({0}) {1}.",
								    (int) code, webResponse.StatusDescription);
					throwMe = new WebException (err, null, protoError, webResponse);
					webResponse.ReadAll ();
				} else if ((int) code == 304 && allowAutoRedirect) {
					string err = String.Format ("The remote server returned an error: ({0}) {1}.",
								    (int) code, webResponse.StatusDescription);
					throwMe = new WebException (err, null, protoError, webResponse);
				} else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) {
					throwMe = new WebException ("Max. redirections exceeded.", null,
								    protoError, webResponse);
					webResponse.ReadAll ();
				}
			}

			bodyBuffer = null;
			if (throwMe == null) {
				bool b = false;
				int c = (int) code;
				if (allowAutoRedirect && c >= 300) {
					if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) {
						bodyBuffer = writeStream.WriteBuffer;
						bodyBufferLength = writeStream.WriteBufferLength;
					}
					b = Redirect (result, code);
					if (b && ntlm_auth_state != 0)
						ntlm_auth_state = 0;
				}

				if (resp != null && c >= 300 && c != 304)
					resp.ReadAll ();

				return b;
			}
				
			if (!ThrowOnError)
				return false;

			if (writeStream != null) {
				writeStream.InternalClose ();
				writeStream = null;
			}

			webResponse = null;

			throw throwMe;
		}
Пример #4
0
        // Returns true if redirected
        bool CheckFinalStatus(WebAsyncResult result)
        {
            if (result.GotException)
            {
                throw result.Exception;
            }

            Exception throwMe = result.Exception;

            bodyBuffer = null;

            HttpWebResponse    resp       = result.Response;
            WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
            HttpStatusCode     code       = 0;

            if (throwMe == null && webResponse != null)
            {
                code = webResponse.StatusCode;
                if (!authCompleted && ((code == HttpStatusCode.Unauthorized && credentials != null) ||
                                       (ProxyQuery && code == HttpStatusCode.ProxyAuthenticationRequired)))
                {
                    if (!usedPreAuth && CheckAuthorization(webResponse, code))
                    {
                        // Keep the written body, so it can be rewritten in the retry
                        if (InternalAllowBuffering)
                        {
                            bodyBuffer       = writeStream.WriteBuffer;
                            bodyBufferLength = writeStream.WriteBufferLength;
                            webResponse.Close();
                            return(true);
                        }
                        else if (method != "PUT" && method != "POST")
                        {
                            webResponse.Close();
                            return(true);
                        }

                        writeStream.InternalClose();
                        writeStream = null;
                        webResponse.Close();
                        webResponse = null;

                        throw new WebException("This request requires buffering " +
                                               "of data for authentication or " +
                                               "redirection to be sucessful.");
                    }
                }

                if ((int)code >= 400)
                {
                    string err = String.Format("The remote server returned an error: ({0}) {1}.",
                                               (int)code, webResponse.StatusDescription);
                    throwMe = new WebException(err, null, protoError, webResponse);
                    webResponse.ReadAll();
                }
                else if ((int)code == 304 && allowAutoRedirect)
                {
                    string err = String.Format("The remote server returned an error: ({0}) {1}.",
                                               (int)code, webResponse.StatusDescription);
                    throwMe = new WebException(err, null, protoError, webResponse);
                }
                else if ((int)code >= 300 && allowAutoRedirect && redirects > maxAutoRedirect)
                {
                    throwMe = new WebException("Max. redirections exceeded.", null,
                                               protoError, webResponse);
                    webResponse.ReadAll();
                }
            }

            if (throwMe == null)
            {
                bool b = false;
                int  c = (int)code;
                if (allowAutoRedirect && c >= 300)
                {
                    if (InternalAllowBuffering && writeStream.WriteBufferLength > 0)
                    {
                        bodyBuffer       = writeStream.WriteBuffer;
                        bodyBufferLength = writeStream.WriteBufferLength;
                    }
                    b = Redirect(result, code);
                }

                if (resp != null && c >= 300 && c != 304)
                {
                    resp.ReadAll();
                }

                return(b);
            }

            if (writeStream != null)
            {
                writeStream.InternalClose();
                writeStream = null;
            }

            webResponse = null;

            throw throwMe;
        }