Esempio n. 1
0
        private void RequestCompleted(IAsyncResult asyncResult)
        {
            AsynHttpContext _context = asyncResult.AsyncState as AsynHttpContext;

            try
            {
                if (asyncResult == null)
                {
                    return;
                }
                _context.AsynResponse = (HttpWebResponse)_context.AsynRequest.EndGetResponse(asyncResult);
            }
            catch (WebException e)
            {
                // _logger.Error(e, "RequestCompleted WebException.");
                _context.AsynResponse = (HttpWebResponse)e.Response;
                if (_context.AsynResponse == null)
                {
                    EndInvoke(_context, e);
                    return;
                }
            }
            catch (Exception e)
            {
                //_logger.Error(e, "RequestCompleted exception.");
                EndInvoke(_context, e);
                return;
            }
            try
            {
                AsynStream stream = new AsynStream(_context.AsynResponse.GetResponseStream());
                stream.Offset = 0;
                long length = _context.AsynResponse.ContentLength;
                if (length < 0)
                {
                    length           = DEFAULT_LENGTH;
                    notContentLength = true;
                }
                buffer = new byte[length];
                stream.UnreadLength = buffer.Length;
                IAsyncResult asyncRead = stream.NetStream.BeginRead(buffer, 0, stream.UnreadLength, new AsyncCallback(ReadCallBack), stream);
            }
            catch (Exception e)
            {
                //  _logger.Error(e, "BeginRead exception.");
                EndInvoke(_context, e);
            }
        }
Esempio n. 2
0
 private void ReadCallBack(IAsyncResult asyncResult)
 {
     try
     {
         AsynStream stream = asyncResult.AsyncState as AsynStream;
         int        read   = 0;
         if (stream.UnreadLength > 0)
         {
             read = stream.NetStream.EndRead(asyncResult);
         }
         if (read > 0)
         {
             stream.Offset       += read;
             stream.UnreadLength -= read;
             stream.Count++;
             if (notContentLength && stream.Offset >= buffer.Length)
             {
                 Array.Resize <byte>(ref buffer, stream.Offset + DEFAULT_POS_LENGTH);
                 stream.UnreadLength = DEFAULT_POS_LENGTH;
             }
             IAsyncResult asyncRead = stream.NetStream.BeginRead(buffer, stream.Offset, stream.UnreadLength, new AsyncCallback(ReadCallBack), stream);
         }
         else
         {
             _watch.Stop();
             stream.NetStream.Dispose();
             if (buffer.Length != stream.Offset)
             {
                 Array.Resize <byte>(ref buffer, stream.Offset);
             }
             _context.ExecTime = _watch.ElapsedMilliseconds;
             _context.SetResponseBytes(buffer);
             _action.Invoke(_context, null);
         }
     }
     catch (Exception e)
     {
         // _logger.Error(e, "ReadCallBack exception.");
         EndInvoke(_context, e);
     }
 }