Esempio n. 1
0
        /// <summary>
        /// Connects to the server specified by the address and port, sends the request and waits for a response.
        /// </summary>
        /// <param name="address">The address of the server, either an IPv4 address or Dns hostname</param>
        /// <param name="port">The port of the server</param>
        /// <param name="request">The request to send</param>
        /// <param name="exception">Any exception that is throw or encountered during the request/response session with the server</param>
        /// <param name="verbose">A flag that indicates the connection's verbosity level, true for more messages</param>
        /// <returns></returns>
        public static HttpResponse GetResponse(
            string address,
            int port,
            HttpRequest request,
            out Exception exception,
            bool verbose,
            AddressResolutionEventHandler onResolvingAddress,
            HttpMessageProgressEventHandler onSendProgress,
            HttpMessageProgressEventHandler onRecvProgress,
            object stateObject)
        {
            try
            {
                // parse the address using either IPv4 or Dns for hostnames into an end point with the port specified
                IPEndPoint ep = HttpUtils.Resolve(address, port, null, onResolvingAddress, stateObject);

                // return a response from the server
                return(HttpRequest.GetResponse(ep, request, out exception, verbose, onSendProgress, onRecvProgress, stateObject));
            }
            catch (ThreadAbortException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Connects to the server specified by the endpoint, sends the request and waits for a response.
        /// </summary>
        /// <param name="ep">The address:port of the server</param>
        /// <param name="request">The request to send</param>
        /// <param name="exception">Any exception that is throw or encountered during the request/response session with the server</param>
        /// <param name="verbose">A flag that indicates the connection's verbosity level, true for more messages</param>
        /// <returns></returns>
        public static HttpResponse GetResponse(
            IPEndPoint ep,
            HttpRequest request,
            out Exception exception,
            bool verbose,
            HttpMessageProgressEventHandler onSendProgress,
            HttpMessageProgressEventHandler onRecvProgress,
            object stateObject)
        {
            #region Params Validation

            if (ep == null)
            {
                throw new ArgumentNullException("ep");
            }

            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            exception = null;

            #endregion

            // define a connection for this request/response session
            HttpConnection connection = null;

            try
            {
                // create a connection to the remote end point
                connection = new HttpConnection(ep, false, verbose, HttpOptions.SendTimeout, HttpOptions.RecvTimeout);

                // return a response from the server
                return(HttpRequest.GetResponse(connection, request, out exception, onSendProgress, onRecvProgress, stateObject));
            }
            catch (ThreadAbortException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                // always try and close the connect up afterwards
                if (connection != null)
                {
                    connection.Close();
                }
            }
            return(null);
        }