Пример #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);
        }
Пример #2
0
        /// <summary>
        /// Resolves an address and port to an IPEndPoint.
        /// </summary>
        /// <param name="address">The address to resolve. My be an IPv4 or IPv6 dotted quad or hex notation, or a valid dns hostname.</param>
        /// <param name="port">The remote port number</param>
        /// <returns></returns>
        public static IPEndPoint Resolve(string address, int port, object sender, AddressResolutionEventHandler onResolving, object stateObject)
        {
            #region Addresss Resolution Events

            if (onResolving != null)
            {
                try
                {
                    onResolving(sender, new AddressResolutionEventArgs(address, port, stateObject));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }

            #endregion

            try
            {
                // first try and parse the address out
                // it may be a IPv4 dotted quad or in IPv6 colon-hex notation
                IPAddress ipAddress = IPAddress.Parse(address);

                // return a new end point without ever hitting dns
                return(new IPEndPoint(ipAddress, port));
            }
            catch (Exception ex)
            {
                // try first then fall back on dns because connecting via ip's should be faster and try to bypass dns all together
                if (ex.GetType() == typeof(System.Threading.ThreadAbortException))
                {
                    throw ex;
                }
            }

            // resolve the address using DNS
            IPHostEntry he = Dns.Resolve(address);

            // create and return a new IP end point based on the address and port
            return(new IPEndPoint(he.AddressList[0], port));
        }
Пример #3
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;
		}
Пример #4
0
		/// <summary>
		/// Resolves an address and port to an IPEndPoint.
		/// </summary>
		/// <param name="address">The address to resolve. My be an IPv4 or IPv6 dotted quad or hex notation, or a valid dns hostname.</param>
		/// <param name="port">The remote port number</param>
		/// <returns></returns>
		public static IPEndPoint Resolve(string address, int port, object sender, AddressResolutionEventHandler onResolving, object stateObject)
		{
			#region Addresss Resolution Events

			if (onResolving != null)
			{
				try
				{
					onResolving(sender, new AddressResolutionEventArgs(address, port, stateObject));
				}
				catch(Exception ex)
				{
					Debug.WriteLine(ex);

				}
			}

			#endregion

			try
			{
				// first try and parse the address out
				// it may be a IPv4 dotted quad or in IPv6 colon-hex notation
				IPAddress ipAddress = IPAddress.Parse(address);

				// return a new end point without ever hitting dns
				return new IPEndPoint(ipAddress, port);
			}
			catch(Exception ex)
			{
				// try first then fall back on dns because connecting via ip's should be faster and try to bypass dns all together
				if (ex.GetType() == typeof(System.Threading.ThreadAbortException))
					throw ex;
			}

			// resolve the address using DNS
			IPHostEntry he = Dns.Resolve(address);

			// create and return a new IP end point based on the address and port
			return new IPEndPoint(he.AddressList[0], port);
		}