示例#1
0
        protected void getRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            RequestToCancel requestToCancel = (RequestToCancel)asynchronousResult.AsyncState;

            if (log.isDebugEnabled())
            {
                log.debug("getRequestStreamCallback(" + requestToCancel);
            }
            try
            {
                HttpWebRequest conn       = requestToCancel.getConnection();
                Stream         postStream = conn.EndGetRequestStream(asynchronousResult);
                if (requestToCancel.buf != null)
                {
                    if (log.isDebugEnabled())
                    {
                        log.debug("bufferToStream");
                    }
                    bufferToStream(requestToCancel.buf, postStream);
                }
                else if (requestToCancel.stream != null)
                {
                    if (log.isDebugEnabled())
                    {
                        log.debug("copy stream");
                    }
                    requestToCancel.stream.CopyTo(postStream);
                    postStream.Close();
                }
                conn.BeginGetResponse(new AsyncCallback(this.getResponseCallback), requestToCancel);
            }
            catch (Exception e)
            {
                if (log.isDebugEnabled())
                {
                    log.debug("Exception:", e);
                }
                BException bex = null;
                if (_cancelAllRequests)
                {
                    bex = new BException(BExceptionC.CANCELLED, "");
                }
                else
                {
                    bex = new BException(BExceptionC.IOERROR, e.Message, e);
                }
                removeRequest(requestToCancel, null, bex);
            }
            finally
            {
                if (requestToCancel.stream != null)
                {
                    requestToCancel.stream.Close();
                }
            }
            if (log.isDebugEnabled())
            {
                log.debug(")getRequestStreamCallback");
            }
        }
示例#2
0
        protected void getResponseCallback(IAsyncResult asynchronousResult)
        {
            RequestToCancel requestToCancel = (RequestToCancel)asynchronousResult.AsyncState;

            if (log.isDebugEnabled())
            {
                log.debug("getResponseCallback(" + requestToCancel);
            }
            HttpWebResponse response        = null;
            ByteBuffer      returnBuffer    = null;
            Exception       returnException = null;
            HttpWebRequest  conn            = requestToCancel.getConnection();

            try
            {
                response = (HttpWebResponse)conn.EndGetResponse(asynchronousResult);
                if (log.isDebugEnabled())
                {
                    log.debug("status=" + response.StatusCode);
                }
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    requestToCancel.setAsyncResult(null, new BException(BExceptionC.IOERROR, "HTTP Status " + response.StatusCode));
                }
                else
                {
                    saveSession(conn);

                    if (log.isDebugEnabled())
                    {
                        log.debug("bufferFromStream");
                    }

                    Stream responseStream = response.GetResponseStream();
                    returnBuffer = bufferFromStream(responseStream, false);
                }
            }
            catch (WebException e)
            {
                BException bex = null;
                if (_cancelAllRequests)
                {
                    bex = new BException(BExceptionC.CANCELLED, "");
                }
                else
                {
                    if (e.Response != null)
                    {
                        int code = Convert.ToInt32(((HttpWebResponse)e.Response).StatusCode);
                        bex = new BException(code, "HTTP Status " + code);
                    }
                    else
                    {
                        int code = Convert.ToInt32(HttpStatusCode.BadRequest);
                        bex = new BException(code, e.ToString());
                    }
                }
                returnException = bex;
            }
            catch (Exception e)
            {
                if (log.isDebugEnabled())
                {
                    log.debug("Exception", e);
                }
                returnException = new BException(BExceptionC.IOERROR, e.Message, e);
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }

                removeRequest(requestToCancel, returnBuffer, returnException);
            }

            if (log.isDebugEnabled())
            {
                log.debug(")getResponseCallback");
            }
        }