Exemplo n.º 1
0
        /// <summary>
        /// Process a http request.
        /// </summary>
        /// <param name="serviceUri">The service name to call.</param>
        /// <param name="callback">The callback handler when a result is retured, a client web response.</param>
        /// <param name="method">The request method.</param>
        /// <param name="input">The stream containing the data to send (can be null).</param>
        /// <param name="credentials">The request network credentials.</param>
        public static void RequestAsync(Uri serviceUri,
                                        Action <Nequeo.Net.NetContext, object> callback, string method = "GET",
                                        System.IO.Stream input = null, NetworkCredential credentials = null)
        {
            // Is the connection secure.
            bool isSecure = false;

            if (serviceUri.Scheme.ToLower() == "https")
            {
                isSecure = true;
            }

            // Open a new connection.
            Nequeo.Net.Client client = new Client(serviceUri, isSecureConnection: isSecure);
            client.IsHttpProtocol = true;

            // Create the request.
            Nequeo.Net.NetRequest request = client.GetRequest();
            request.Method        = method;
            request.ContentLength = (input != null ? input.Length : 0);

            if (credentials != null)
            {
                request.Credentials = credentials;
            }

            // Send the data.
            client.Transfer(request, input, CancellationToken.None, callback, client);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Process the request.
        /// </summary>
        /// <typeparam name="T">The type to return.</typeparam>
        /// <param name="serviceName">The service name to call.</param>
        /// <param name="query">The query to apply.</param>
        /// <returns>The returned type.</returns>
        private T ProcessRequest <T>(string serviceName, string query)
        {
            // Is the connection secure.
            bool isSecure = false;

            if (_serviceRoot.Scheme.ToLower() == "https")
            {
                isSecure = true;
            }

            // Construct the URI.
            Uri constructedServiceUri = new Uri(_serviceRoot.AbsoluteUri.TrimEnd(new char[] { '/' }) + "/" + serviceName + "?" + query.TrimStart(new char[] { '&' }));
            T   data = default(T);

            // Open a new connection.
            using (Nequeo.Net.Client client = new Client(constructedServiceUri, isSecureConnection: isSecure))
            {
                client.IsHttpProtocol = true;

                // Create the request.
                Nequeo.Net.NetRequest request = client.GetRequest();
                request.Method        = "GET";
                request.ContentLength = 0;
                client.Transfer(request, null, CancellationToken.None);
                Nequeo.Net.NetContext context = client.GetContext();

                // Read all the headers.
                string resources = null;
                List <Model.NameValue> headers = Nequeo.Net.Utility.ParseHeaders(context.NetResponse.Input, out resources, client.NetClient.ResponseTimeout);

                // Parse the headers and the resource response.
                context.NetResponse.ReadNetResponseHeaders(headers, resources);

                // Open a stream.
                using (MemoryStream stream = new MemoryStream())
                {
                    // If sending chunked.
                    if (context.NetResponse.SendChunked)
                    {
                        Nequeo.Net.Utility.ReadChunkedData(context.NetResponse.Input, stream, client.NetClient.ResponseTimeout);
                    }
                    else
                    {
                        // Copy the response stream.
                        Nequeo.IO.Stream.Operation.CopyStream(context.NetResponse.Input, stream, context.NetResponse.ContentLength, client.NetClient.ResponseTimeout);
                    }

                    // Create the object from the json data.
                    string json = Encoding.UTF8.GetString(stream.ToArray());
                    data = Nequeo.Serialisation.JavaObjectNotation.Deserializer <T>(json);
                }
            }

            // Data.
            return(data);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Process a http request.
        /// </summary>
        /// <typeparam name="T">The type to return.</typeparam>
        /// <param name="serviceUri">The service name to call.</param>
        /// <param name="request">The request provider used to send the data.</param>
        /// <param name="input">The stream containing the data to send (can be null).</param>
        /// <returns>The returned type.</returns>
        public static T Request <T>(Uri serviceUri, Nequeo.Net.NetRequest request, System.IO.Stream input = null)
        {
            // Is the connection secure.
            bool isSecure = false;

            if (serviceUri.Scheme.ToLower() == "https")
            {
                isSecure = true;
            }

            T data = default(T);

            // Open a new connection.
            using (Nequeo.Net.Client client = new Client(serviceUri, isSecureConnection: isSecure))
            {
                client.IsHttpProtocol = true;
                request.Output        = client.GetRequest().Output;

                // Send the data.
                client.Transfer(request, input, CancellationToken.None);

                // Get the response context.
                Nequeo.Net.NetContext context = client.GetContext();

                // Read all the headers.
                string resources = null;
                List <Model.NameValue> headers = Nequeo.Net.Utility.ParseHeaders(context.NetResponse.Input, out resources, client.NetClient.ResponseTimeout);

                // Parse the headers and the resource response.
                context.NetResponse.ReadNetResponseHeaders(headers, resources);

                // Open a stream.
                using (MemoryStream stream = new MemoryStream())
                {
                    // If sending chunked.
                    if (context.NetResponse.SendChunked)
                    {
                        Nequeo.Net.Utility.ReadChunkedData(context.NetResponse.Input, stream, client.NetClient.ResponseTimeout);
                    }
                    else
                    {
                        // Copy the response stream.
                        Nequeo.IO.Stream.Operation.CopyStream(context.NetResponse.Input, stream, context.NetResponse.ContentLength, client.NetClient.ResponseTimeout);
                    }

                    // Create the object from the json data.
                    string json = Encoding.UTF8.GetString(stream.ToArray());
                    data = Nequeo.Serialisation.JavaObjectNotation.Deserializer <T>(json);
                }
            }

            // Data.
            return(data);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Process a general data request.
        /// </summary>
        /// <param name="serviceUri">The service name to call.</param>
        /// <param name="callback">The callback handler when a result is retured, a client web response.</param>
        /// <param name="input">The stream containing the data to send (can be null).</param>
        /// <param name="byteLength">The total number of bytes that need to be read (must be the same as the number of input bytes).</param>
        /// <param name="isSecure">True if the connection is secure.</param>
        public static void RequestAsync(Uri serviceUri, Action <Nequeo.Net.NetContext, object> callback,
                                        System.IO.Stream input, long byteLength, bool isSecure = false)
        {
            // Open a new connection.
            Nequeo.Net.Client client = new Client(serviceUri, isSecureConnection: isSecure);
            client.IsHttpProtocol = false;

            // Create a new request.
            Nequeo.Net.NetRequest request = client.GetRequest();
            request.ContentLength = byteLength;

            // Send the data.
            client.Transfer(request, input, CancellationToken.None, callback, client);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Process a http request.
        /// </summary>
        /// <param name="serviceUri">The service name to call.</param>
        /// <param name="request">The request provider used to send the data.</param>
        /// <param name="callback">The callback handler when a result is retured, a client web response.</param>
        /// <param name="input">The stream containing the data to send (can be null).</param>
        public static void RequestAsync(Uri serviceUri, Nequeo.Net.NetRequest request,
                                        Action <Nequeo.Net.NetContext, object> callback, System.IO.Stream input = null)
        {
            // Is the connection secure.
            bool isSecure = false;

            if (serviceUri.Scheme.ToLower() == "https")
            {
                isSecure = true;
            }

            // Open a new connection.
            Nequeo.Net.Client client = new Client(serviceUri, isSecureConnection: isSecure);
            client.IsHttpProtocol = true;
            request.Output        = client.GetRequest().Output;

            // Send the data.
            client.Transfer(request, input, CancellationToken.None, callback, client);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the status for the certificate.
        /// </summary>
        /// <param name="certificates">The certificates to get status for.</param>
        /// <param name="callback">The callback handler when a result is retured, a client web response.</param>
        /// <param name="resourcePath">The full path to the remote resource.</param>
        /// <param name="actionName">The current action callback name.</param>
        /// <param name="state">The state callback handler object.</param>
        /// <exception cref="System.Exception"></exception>
        /// <exception cref="System.ArgumentNullException"></exception>
        public void CertificateStatus(X509Certificate2[] certificates, Action <List <string>, string, List <byte[]>, object> callback,
                                      string resourcePath = "/ocsp", string actionName = "", object state = null)
        {
            // If not connected.
            if (!base.Connected)
            {
                throw new Exception("A connection does not exist, create a connection first.");
            }

            // If connected.
            if (base.Connected)
            {
                // Only allow one request at a time.
                lock (_lockRequestObject)
                {
                    // Set the default action name.
                    string actName = (String.IsNullOrEmpty(actionName) ? "OCSP_CS" : actionName);

                    // Assign the call back.
                    _callback[actName] = callback;
                    _state[actName]    = state;
                    _actionNameReference["OCSP_CS"] = actName;

                    // Get the ocsp request.
                    byte[] ocspData = GetOcspRequest(certificates);

                    // Write the request to the server.
                    Nequeo.Net.NetRequest request = base.GetRequest();
                    request.Method        = "POST";
                    request.ContentLength = (long)ocspData.Length;
                    request.KeepAlive     = true;
                    request.Host          = base.HostNameOrAddress + ":" + base.Port.ToString();
                    request.Path          = resourcePath;
                    request.ContentType   = "application/ocsp-request";
                    request.AddHeader("Member", "OCSP_CS");
                    request.AddHeader("ActionName", actName);
                    request.WriteNetRequestHeaders();
                    request.Write(ocspData);
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Process a http request.
        /// </summary>
        /// <typeparam name="T">The type to return.</typeparam>
        /// <param name="serviceUri">The service name to call.</param>
        /// <param name="headerList">The array of response headers.</param>
        /// <param name="response">The response message.</param>
        /// <param name="method">The request method.</param>
        /// <param name="input">The stream containing the data to send (can be null).</param>
        /// <param name="credentials">The request network credentials.</param>
        /// <returns>The returned type.</returns>
        public static T Request <T>(Uri serviceUri,
                                    out Model.NameValue[] headerList, out Nequeo.Model.Message.ResponseResource response,
                                    string method = "GET", System.IO.Stream input = null, NetworkCredential credentials = null)
        {
            // Is the connection secure.
            bool isSecure = false;

            if (serviceUri.Scheme.ToLower() == "https")
            {
                isSecure = true;
            }

            T data = default(T);

            // Open a new connection.
            using (Nequeo.Net.Client client = new Client(serviceUri, isSecureConnection: isSecure))
            {
                client.IsHttpProtocol = true;

                // Create the request.
                Nequeo.Net.NetRequest request = client.GetRequest();
                request.Method        = method;
                request.ContentLength = (input != null ? input.Length : 0);

                if (credentials != null)
                {
                    request.Credentials = credentials;
                }

                // Send the data.
                client.Transfer(request, input, CancellationToken.None);

                // Get the response context.
                Nequeo.Net.NetContext context = client.GetContext();

                // Read all the headers.
                string resources = null;
                List <Model.NameValue> headers = Nequeo.Net.Utility.ParseHeaders(context.NetResponse.Input, out resources, client.NetClient.ResponseTimeout);
                headerList = headers.ToArray();

                // Parse the headers and the resource response.
                context.NetResponse.ReadNetResponseHeaders(headers, resources);
                response = new Model.Message.ResponseResource()
                {
                    Code            = context.NetResponse.StatusCode,
                    Subcode         = context.NetResponse.StatusSubcode,
                    Description     = context.NetResponse.StatusDescription,
                    ProtocolVersion = context.NetResponse.ProtocolVersion
                };

                // Open a stream.
                using (MemoryStream stream = new MemoryStream())
                {
                    // If sending chunked.
                    if (context.NetResponse.SendChunked)
                    {
                        Nequeo.Net.Utility.ReadChunkedData(context.NetResponse.Input, stream, client.NetClient.ResponseTimeout);
                    }
                    else
                    {
                        // Copy the response stream.
                        Nequeo.IO.Stream.Operation.CopyStream(context.NetResponse.Input, stream, context.NetResponse.ContentLength, client.NetClient.ResponseTimeout);
                    }

                    // Create the object from the json data.
                    string json = Encoding.UTF8.GetString(stream.ToArray());
                    data = Nequeo.Serialisation.JavaObjectNotation.Deserializer <T>(json);
                }
            }

            // Data.
            return(data);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Process a http request.
 /// </summary>
 /// <param name="serviceUri">The service name to call.</param>
 /// <param name="request">The request provider used to send the data.</param>
 /// <param name="callback">The callback handler when a result is retured, a client web response.</param>
 /// <param name="input">The stream containing the data to send (can be null).</param>
 public static void HttpRequestAsync(Uri serviceUri, Nequeo.Net.NetRequest request,
                                     Action <Nequeo.Net.NetContext, object> callback, System.IO.Stream input = null)
 {
     Nequeo.Net.HttpDataClient.RequestAsync(serviceUri, request, callback, input);
 }
Exemplo n.º 9
0
        /// <summary>
        /// Transfer the data.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="input">The stream containing the data to send (can be null).</param>
        /// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
        /// <param name="callback">The callback handler when a result is retured, a client web response.</param>
        /// <param name="state">The state callback handler object.</param>
        public virtual void Transfer(Nequeo.Net.NetRequest request, System.IO.Stream input,
                                     CancellationToken cancellationToken, Action <Nequeo.Net.NetContext, object> callback, object state = null)
        {
            if (_client == null)
            {
                throw new Exception("A connection does not exist.");
            }

            // If not connected.
            if (!_client.Connected)
            {
                _client.Connect();
            }

            // If connected.
            if (_client.Connected)
            {
                // Only allow one request at a time.
                lock (_lockRequestObject)
                {
                    // Assign the call back.
                    _callback["Transfer"] = callback;
                    _state["Transfer"]    = state;

                    // Is http protocol (uses headers).
                    if (_isHttpProtocol)
                    {
                        // If no host has been set.
                        if (String.IsNullOrEmpty(request.Host))
                        {
                            request.Host = _client.HostNameOrAddress + ":" + _client.Port.ToString();
                        }

                        // If no path has been set.
                        if (String.IsNullOrEmpty(request.Path))
                        {
                            request.Path = (_requestUri != null ? _requestUri.PathAndQuery : "/");
                        }
                        else
                        {
                            // If path is at root.
                            if (request.Path.Equals("/"))
                            {
                                request.Path = (_requestUri != null ? _requestUri.PathAndQuery : "/");
                            }
                        }

                        // Write the headers.
                        request.WriteNetRequestHeaders();
                    }

                    // If data exists.
                    if (input != null)
                    {
                        // Write data.
                        if (input.Length > 0 && request.ContentLength > 0)
                        {
                            // Send the data.
                            Nequeo.IO.Stream.Operation.CopyStream(input, request.Output, cancellationToken, request.ContentLength, _client.RequestTimeout, _client.WriteBufferSize);
                        }
                        else
                        {
                            // If one or the other is zero.
                            if (input.Length == 0 || request.ContentLength == 0)
                            {
                                throw new Exception("The request content length is zero or the input length is zero.");
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Process a http request.
        /// </summary>
        /// <param name="serviceUri">The service name to call.</param>
        /// <param name="method">The request method.</param>
        /// <param name="input">The stream containing the data to send (can be null).</param>
        /// <param name="credentials">The request network credentials.</param>
        /// <returns>The array of bytes; else null.</returns>
        public static byte[] Request(Uri serviceUri, string method = "GET",
                                     System.IO.Stream input        = null, NetworkCredential credentials = null)
        {
            // Is the connection secure.
            bool isSecure = false;

            if (serviceUri.Scheme.ToLower() == "https")
            {
                isSecure = true;
            }

            byte[] data = null;

            // Open a new connection.
            using (Nequeo.Net.Client client = new Client(serviceUri, isSecureConnection: isSecure))
            {
                client.IsHttpProtocol = true;

                // Create the request.
                Nequeo.Net.NetRequest request = client.GetRequest();
                request.Method        = method;
                request.ContentLength = (input != null ? input.Length : 0);

                if (credentials != null)
                {
                    request.Credentials = credentials;
                }

                // Send the data.
                client.Transfer(request, input, CancellationToken.None);

                // Get the response context.
                Nequeo.Net.NetContext context = client.GetContext();

                // Read all the headers.
                string resources = null;
                List <Model.NameValue> headers = Nequeo.Net.Utility.ParseHeaders(context.NetResponse.Input, out resources, client.NetClient.ResponseTimeout);

                // Parse the headers and the resource response.
                context.NetResponse.ReadNetResponseHeaders(headers, resources);

                // Open a stream.
                using (MemoryStream stream = new MemoryStream())
                {
                    // If sending chunked.
                    if (context.NetResponse.SendChunked)
                    {
                        Nequeo.Net.Utility.ReadChunkedData(context.NetResponse.Input, stream, client.NetClient.ResponseTimeout);
                    }
                    else
                    {
                        // Copy the response stream.
                        Nequeo.IO.Stream.Operation.CopyStream(context.NetResponse.Input, stream, context.NetResponse.ContentLength, client.NetClient.ResponseTimeout);
                    }

                    // Create the object from the data.
                    data = stream.ToArray();
                }
            }

            // Data.
            return(data);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Process a http request.
        /// </summary>
        /// <param name="serviceUri">The service name to call.</param>
        /// <param name="request">The request provider used to send the data.</param>
        /// <param name="headerList">The array of response headers.</param>
        /// <param name="response">The response message.</param>
        /// <param name="input">The stream containing the data to send (can be null).</param>
        /// <returns>The array of bytes; else null.</returns>
        public static byte[] Request(Uri serviceUri, Nequeo.Net.NetRequest request,
                                     out Model.NameValue[] headerList, out Nequeo.Model.Message.ResponseResource response,
                                     System.IO.Stream input = null)
        {
            // Is the connection secure.
            bool isSecure = false;

            if (serviceUri.Scheme.ToLower() == "https")
            {
                isSecure = true;
            }

            byte[] data = null;

            // Open a new connection.
            using (Nequeo.Net.Client client = new Client(serviceUri, isSecureConnection: isSecure))
            {
                client.IsHttpProtocol = true;
                request.Output        = client.GetRequest().Output;

                // Send the data.
                client.Transfer(request, input, CancellationToken.None);

                // Get the response context.
                Nequeo.Net.NetContext context = client.GetContext();

                // Read all the headers.
                string resources = null;
                List <Model.NameValue> headers = Nequeo.Net.Utility.ParseHeaders(context.NetResponse.Input, out resources, client.NetClient.ResponseTimeout);
                headerList = headers.ToArray();

                // Parse the headers and the resource response.
                context.NetResponse.ReadNetResponseHeaders(headers, resources);
                response = new Model.Message.ResponseResource()
                {
                    Code            = context.NetResponse.StatusCode,
                    Subcode         = context.NetResponse.StatusSubcode,
                    Description     = context.NetResponse.StatusDescription,
                    ProtocolVersion = context.NetResponse.ProtocolVersion
                };

                // Open a stream.
                using (MemoryStream stream = new MemoryStream())
                {
                    // If sending chunked.
                    if (context.NetResponse.SendChunked)
                    {
                        Nequeo.Net.Utility.ReadChunkedData(context.NetResponse.Input, stream, client.NetClient.ResponseTimeout);
                    }
                    else
                    {
                        // Copy the response stream.
                        Nequeo.IO.Stream.Operation.CopyStream(context.NetResponse.Input, stream, context.NetResponse.ContentLength, client.NetClient.ResponseTimeout);
                    }

                    // Create the object from the data.
                    data = stream.ToArray();
                }
            }

            // Data.
            return(data);
        }