示例#1
0
        public HttpResponse Send(HttpRequest req)
        {
            if (_activeRequest != null)
                throw new InvalidOperationException("A request is already outstanding");

            _activeRequest = req;
            _requestCompletedEvent.Reset();

            using (var socket = GetSocket(req.Uri.Host, req.Uri.Port))
            {
                try
                {
                    StringBuilder buffer = new StringBuilder();

                    req.AppendMethod(buffer);
                    req.AppendHeaders(buffer);

                    socket.Send(buffer.ToString());
                    if (req.Body != null && req.Body.Length > 0)
                        socket.Send(req.Body);

                    _requestCompletedEvent.WaitOne();
                }
                finally
                {
                    socket.SocketClosed -= SocketOnSocketClosed;
                    socket.DataReceived -= SocketOnDataReceived;
                }
            }
            return _lastResponse;
        }
示例#2
0
        public void SendAsync(HttpRequest req)
        {
            if (_activeRequest != null)
                throw new InvalidOperationException("A request is already outstanding");

            _activeRequest = req;

            _socket = GetSocket(req.Uri.Host, req.Uri.Port);

            StringBuilder buffer = new StringBuilder();

            req.AppendMethod(buffer);
            req.AppendHeaders(buffer);

            _socket.Send(buffer.ToString());
            if (req.Body != null && req.Body.Length > 0)
                _socket.Send(req.Body);
        }