コード例 #1
0
        /// <summary>
        /// Process a http response.
        /// </summary>
        /// <typeparam name="T">The type to return.</typeparam>
        /// <param name="context">The client context response.</param>
        /// <param name="headerList">The array of response headers.</param>
        /// <param name="response">The response message.</param>
        /// <param name="timeout">The maximum time in milliseconds to wait for the end of the header data; -1 wait Indefinitely.</param>
        /// <returns>The returned type.</returns>
        public static T ResponseAsync <T>(Nequeo.Net.NetContext context,
                                          out Model.NameValue[] headerList, out Nequeo.Model.Message.ResponseResource response,
                                          long timeout = -1)
        {
            T data = default(T);

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

            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, timeout);
                }
                else
                {
                    // Copy the response stream.
                    Nequeo.IO.Stream.Operation.CopyStream(context.NetResponse.Input, stream, context.NetResponse.ContentLength, timeout);
                }

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

            // Data.
            return(data);
        }
コード例 #2
0
ファイル: Client.cs プロジェクト: waffle-iron/nequeo
 /// <summary>
 /// Process a http response.
 /// </summary>
 /// <param name="context">The client context response.</param>
 /// <param name="headerList">The array of response headers.</param>
 /// <param name="response">The response message.</param>
 /// <param name="timeout">The maximum time in milliseconds to wait for the end of the header data; -1 wait Indefinitely.</param>
 /// <returns>The array of bytes; else null.</returns>
 public static byte[] HttpResponseAsync(Nequeo.Net.NetContext context,
                                        out Model.NameValue[] headerList, out Nequeo.Model.Message.ResponseResource response,
                                        long timeout = -1)
 {
     return(Nequeo.Net.HttpDataClient.ResponseAsync(context, out headerList, out response, timeout));
 }
コード例 #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="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 returned type.</returns>
        public static T Request <T>(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;
            }

            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);
                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);
        }
コード例 #4
0
ファイル: DataClient.cs プロジェクト: waffle-iron/nequeo
        /// <summary>
        /// Process a http request.
        /// </summary>
        /// <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 array of bytes; else null.</returns>
        public static byte[] Request(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;
            }

            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);
                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);
        }