/// <summary> /// Process a http response. /// </summary> /// <param name="context">The client context response.</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[] ResponseAsync(Nequeo.Net.NetContext context, long timeout = -1) { byte[] data = null; // Read all the headers. string resources = null; List <Model.NameValue> headers = Nequeo.Net.Utility.ParseHeaders(context.NetResponse.Input, out resources, timeout); // 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, timeout); } else { // Copy the response stream. Nequeo.IO.Stream.Operation.CopyStream(context.NetResponse.Input, stream, context.NetResponse.ContentLength, timeout); } // Create the object from the data. data = stream.ToArray(); } // Data. return(data); }
/// <summary> /// Process a http response. /// </summary> /// <typeparam name="T">The type to return.</typeparam> /// <param name="context">The client context response.</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, 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); // 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, 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); }
/// <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); }
/// <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); }
/// <summary> /// Process a general data response. /// </summary> /// <param name="context">The client context response.</param> /// <param name="byteLength">The total number of bytes that need to be read (must be the same as the number of source bytes). Waits until all bytes are read.</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[] ResponseAsync(Nequeo.Net.NetContext context, long byteLength, long timeout = -1) { byte[] data = null; // Open a stream. using (MemoryStream stream = new MemoryStream()) { // Copy the response stream. Nequeo.IO.Stream.Operation.CopyStream(context.NetResponse.Input, stream, byteLength, timeout); // Create the object from the data. data = stream.ToArray(); } // Data. return(data); }
/// <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 xml data. data = Nequeo.Serialisation.GenericSerialisation <T> .Instance.Deserialise(stream.ToArray()); } // Data. return(data); }
/// <summary> /// Create the web context. /// </summary> /// <param name="context">The current client context.</param> /// <returns>The net context.</returns> protected virtual Nequeo.Net.NetContext CreateNetContext(Nequeo.Net.Provider.ClientContext context) { // If the context has not been created. if (context.ContextState == null) { // Create the new context. Nequeo.Net.NetContext netContext = new Nequeo.Net.NetContext(); // Assign the current context. netContext.IsSecureConnection = context.IsSecureConnection; netContext.IsSslAuthenticated = context.IsSslAuthenticated; netContext.Port = context.Port; netContext.ServiceName = context.ServiceName; netContext.UniqueIdentifier = context.UniqueIdentifier; netContext.IsAuthenticated = false; netContext.IsStartOfConnection = true; netContext.SocketState = context.SocketState; netContext.IsAsyncMode = context.IsAsyncMode; netContext.ConnectionID = context.ConnectionID; // Assign the request input stream and response output stream. AssignRequestResponseStreams(context, netContext); // Assign the current context. context.ContextState = netContext; } else { // Get the current context. Nequeo.Net.NetContext netContext = (Nequeo.Net.NetContext)context.ContextState; netContext.ServiceName = context.ServiceName; netContext.UniqueIdentifier = context.UniqueIdentifier; netContext.IsAuthenticated = context.IsAuthenticated; netContext.IsStartOfConnection = false; // Assign the current context. context.ContextState = netContext; } // Return the request context. return((Nequeo.Net.NetContext)context.ContextState); }
/// <summary> /// On received action handler. /// </summary> /// <param name="context">The current client context.</param> private void OnReceivedActionHandler(Nequeo.Net.Provider.ClientContext context) { try { // Create the web context and set the headers. Nequeo.Net.NetContext netContext = CreateNetContext(context); // If the event has been assigned. if (OnNetContext != null) { OnNetContext(this, netContext); } // Save the web context state objects. SaveNetContext(context, netContext); } catch (Exception) { // Close the connection and release all resources used for communication. context.Close(); } }
/// <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); }
/// <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)); }
/// <summary> /// Process a http response. /// </summary> /// <param name="context">The client context response.</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, long timeout = -1) { return(Nequeo.Net.HttpDataClient.ResponseAsync(context, timeout)); }
/// <summary> /// Save the web context state objects. /// </summary> /// <param name="context">The current server context.</param> /// <param name="netContext">The current web context.</param> protected virtual void SaveNetContext(Nequeo.Net.Provider.ClientContext context, Nequeo.Net.NetContext netContext) { // Assign the state objects. context.UniqueIdentifier = netContext.UniqueIdentifier; context.ServiceName = netContext.ServiceName; context.IsAuthenticated = netContext.IsAuthenticated; }
/// <summary> /// Assign the request output stream and response input stream. /// </summary> /// <param name="context">The current server context.</param> /// <param name="netContext">The current web context.</param> protected virtual void AssignRequestResponseStreams(Nequeo.Net.Provider.ClientContext context, Nequeo.Net.NetContext netContext) { // Create the response and request objects. netContext.NetResponse = Nequeo.Net.NetResponse.Create(context.Response.Input); netContext.NetRequest = Nequeo.Net.NetRequest.Create(context.Request.Output); }
/// <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); }
/// <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); }