Пример #1
0
		public override WebResponse GetResponse()
		{
			lock(this)
			{
				if (_isAborted)
					throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
				if(!_isConnectionOpened)
					OpenConnection();
				if(_response == null)
				{
					try
					{	
						synchHeaders();
						InternalExecuteMethod ();						
						int numOfRedirects = 0;
						while (isRedirectNeeded (_method) && _allowAutoRedirect && numOfRedirects < MaxAutoRedirections) {
							if (!HandleManualyRedirect ())
								break;
							numOfRedirects++;
						}
						
						//todo right place to re-put all headers again...
						mainsoft.apache.commons.httpclient.Header hostHeader =
							_method.getRequestHeader("Host");
						if(hostHeader != null)
							Headers.SetInternal("Host", hostHeader.getValue());

						_response = new HttpWebResponse(_method, _state, _stateCache, GetAddress(), this.MethodName);
						
						if(_response != null && 
							_response.Cookies != null && 
							_response.Cookies.Count > 0)
						{
							if(CookieContainer != null)
							{
								foreach(Cookie cooky in _response.Cookies)
								{
									CookieContainer.Add(GetAddress(), cooky);
								}
							}
						}

						_hasResponse = true;
						int respCodeAsInt = (int) _response.StatusCode;
						if(respCodeAsInt >= 400)
						{
							// The WebException contains the readable (not closed) response stream.
							// So, in case of WebException, we should read all data from the 
							// network response stream into the memory stream, and after that
							// close the underlying network stream. The following requests to read
							// from the stream will actually read from the memory stream.
							// So, the this.Abort() should not be called in this case.
							_response.ReadAllAndClose();
							//this.Abort();
							throw new WebException("The remote server returned an error: (" + respCodeAsInt +") " +_response.StatusCode, null, WebExceptionStatus.ProtocolError, _response);
						}
						Header location = _method.getResponseHeader ("location");
						if (isRedirectNeeded (_method) && location == null && _method.getFollowRedirects ())
						{
							// See comments above for the error >= 400
							_response.ReadAllAndClose();
							//this.Abort();
							throw new WebException("Got response code "+_response.StatusCode+", but no location provided", null, WebExceptionStatus.ProtocolError, _response);
						}
					}
					catch(ProtocolException e)
					{
						throw new WebException("", e);
					}
					catch(java.net.ConnectException e)
					{
						throw new WebException("Unable to connect to the remote server.", e);
					}
					catch(java.net.SocketTimeoutException e)
					{
						throw new WebException("Timeout exceeded", e);
					}
					catch(java.io.IOException e)
					{
						throw new WebException("", e);
					}
				}
				return _response;
			}

		}
Пример #2
0
        public override WebResponse GetResponse()
        {
            lock (this)
            {
                if (_isAborted)
                {
                    throw new WebException("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
                }
                if (!_isConnectionOpened)
                {
                    OpenConnection();
                }
                if (_response == null)
                {
                    try
                    {
                        synchHeaders();
                        InternalExecuteMethod();
                        int numOfRedirects = 0;
                        while (isRedirectNeeded(_method) && _allowAutoRedirect && numOfRedirects < MaxAutoRedirections)
                        {
                            if (!HandleManualyRedirect())
                            {
                                break;
                            }
                            numOfRedirects++;
                        }

                        //todo right place to re-put all headers again...
                        mainsoft.apache.commons.httpclient.Header hostHeader =
                            _method.getRequestHeader("Host");
                        if (hostHeader != null)
                        {
                            Headers.SetInternal("Host", hostHeader.getValue());
                        }

                        _response = new HttpWebResponse(_method, _state, _stateCache, GetAddress(), this.MethodName);

                        if (_response != null &&
                            _response.Cookies != null &&
                            _response.Cookies.Count > 0)
                        {
                            if (CookieContainer != null)
                            {
                                foreach (Cookie cooky in _response.Cookies)
                                {
                                    CookieContainer.Add(GetAddress(), cooky);
                                }
                            }
                        }

                        _hasResponse = true;
                        int respCodeAsInt = (int)_response.StatusCode;
                        if (respCodeAsInt >= 400)
                        {
                            // The WebException contains the readable (not closed) response stream.
                            // So, in case of WebException, we should read all data from the
                            // network response stream into the memory stream, and after that
                            // close the underlying network stream. The following requests to read
                            // from the stream will actually read from the memory stream.
                            // So, the this.Abort() should not be called in this case.
                            _response.ReadAllAndClose();
                            //this.Abort();
                            throw new WebException("The remote server returned an error: (" + respCodeAsInt + ") " + _response.StatusCode, null, WebExceptionStatus.ProtocolError, _response);
                        }
                        Header location = _method.getResponseHeader("location");
                        if (isRedirectNeeded(_method) && location == null && _method.getFollowRedirects())
                        {
                            // See comments above for the error >= 400
                            _response.ReadAllAndClose();
                            //this.Abort();
                            throw new WebException("Got response code " + _response.StatusCode + ", but no location provided", null, WebExceptionStatus.ProtocolError, _response);
                        }
                    }
                    catch (ProtocolException e)
                    {
                        throw new WebException("", e);
                    }
                    catch (java.net.ConnectException e)
                    {
                        throw new WebException("Unable to connect to the remote server.", e);
                    }
                    catch (java.net.SocketTimeoutException e)
                    {
                        throw new WebException("Timeout exceeded", e);
                    }
                    catch (java.io.IOException e)
                    {
                        throw new WebException("", e);
                    }
                }
                return(_response);
            }
        }