private int dispatch(HttpWebRequest webRequest, Authenticator authenticator, HttpResponseHandler responseHandler)
        {

            HttpWebResponse webResponse;
            try
            {
                webResponse = (HttpWebResponse)webRequest.GetResponse();
            }
            catch (WebException e)
            {
                webResponse = (HttpWebResponse)e.Response;
            }

            Stream entityStream = null;
            try
            {
                entityStream = webResponse.GetResponseStream();

                WebHeaderCollection responseHeaders = webResponse.Headers;
                if (null != authenticator)
                {
                    authenticator.handleHttpResponseHeaders(responseHeaders);
                }

                int statusCode = (int)webResponse.StatusCode;

                if (statusCode >= 200 && statusCode < 300)
                {
                    // all is well
                    if (null == entityStream) // e.g. http 204 
                    {
                        responseHandler.handleResponseEntity(responseHeaders, null);
                    }
                    else
                    {
                        long contentLength = webResponse.ContentLength;
                        log.debug(contentLength, "contentLength");

                        StreamEntity responseEntity = new StreamEntity(contentLength, entityStream);
                        responseHandler.handleResponseEntity(responseHeaders, responseEntity);
                    }
                }
                return statusCode;

            }
            finally
            {
                if (null != entityStream)
                {
                    StreamHelper.close(entityStream, false, this);
                }
            }
        }
        public void post(HttpRequestAdapter requestAdapter, HttpResponseHandler responseAdapter)
        {
            HttpWebRequest request = buildPostRequest(requestAdapter, null);
            int statusCode = dispatch(request, null, responseAdapter);

            if (statusCode < 200 || statusCode > 299)
            {
                BaseException e = new BaseException(this, HttpStatus.getReason(statusCode));
                e.FaultCode = statusCode;
                String requestUri = requestAdapter.RequestUri;
                e.addContext("requestUri", requestUri);
                throw e;
            }

        }
        ////////////////////////////////////////////////////////////////////////////

        public void get(HttpRequestAdapter requestAdapter, Authenticator authenticator, HttpResponseHandler responseAdapter)
        {
            HttpWebRequest request = buildGetRequest(requestAdapter, authenticator);
            int statusCode = dispatch(request, authenticator, responseAdapter);

            if (401 == statusCode)
            {
                request = buildGetRequest(requestAdapter, authenticator);
                statusCode = dispatch(request, authenticator, responseAdapter);
            }

            if (statusCode < 200 || statusCode > 299)
            {
                BaseException e = new BaseException(this, HttpStatus.getReason(statusCode));
                e.FaultCode = statusCode;
                String requestUri = requestAdapter.RequestUri;
                e.addContext("requestUri", requestUri);
                throw e;
            }

        }