/// <summary>
        /// Send the request, given content is serialized in JSON into the request body
        /// </summary>
        public ResponseEx SendAsJson <TContent>(TContent content = null, int?timeout = null)
            where TContent : class
        {
            ResponseEx result = null;

            // prepare the request and force JSON
            string errorMessage;
            var    request = InnerPrepareRequest(new ContentType("application/json"), timeout, (content != null), out errorMessage);

            // if we have a request
            if ((request != null)
                // and no error message
                && string.IsNullOrEmpty(errorMessage))
            {
                // if we got a content
                if (content != null)
                {
                    // get the encoding
                    var encoding = (this._encoding ?? EncodingDefault);
                    // open the stream
                    using (var stream = request.GetRequestStream())
                    {
                        // serialize
                        if (!SerializerForJson.Serialize(stream, content, encoding))
                        {
                            // if serialization failed
                            errorMessage = "JSON serialization failed";
                        }
                    }
                }
            }

            // if we have a request
            if ((request != null)
                // and no error message
                && string.IsNullOrEmpty(errorMessage))
            {
                // then send the request and get the response
                result = ResponseEx.SendRequest(request);
            }
            // if we had an error at some point
            else
            {
                // then this is a failure
                result = ResponseEx.Failure(this._url, errorMessage);
            }
            return(result);
        }
        /// <summary>
        /// Send the request, get the result as a string
        /// </summary>
        public ResponseEx Send(string content = null, int?timeout = null)
        {
            ResponseEx result = null;

            // prepare the request
            string errorMessage;
            var    request = InnerPrepareRequest(null, timeout, !string.IsNullOrEmpty(content), out errorMessage);

            // if we have a request
            if ((request != null)
                // and no error message
                && string.IsNullOrEmpty(errorMessage))
            {
                // if we got a content
                if (!string.IsNullOrEmpty(content))
                {
                    // we really need an encoding here
                    var encoding = (this._encoding ?? EncodingDefault);
                    // convert content as a byte array
                    var buffer = encoding.GetBytes(content);
                    // set length
                    request.ContentLength = buffer.Length;
                    // open a stream
                    using (var stream = request.GetRequestStream())
                    {
                        // push the byte array
                        stream.Write(buffer, 0, buffer.Length);
                    }
                }
            }

            // if we have a request
            if ((request != null)
                // and no error message
                && string.IsNullOrEmpty(errorMessage))
            {
                // then send the request and get the response
                result = ResponseEx.SendRequest(request);
            }
            // if we had an error at some point
            else
            {
                // then this is a failure
                result = ResponseEx.Failure(this._url, errorMessage);
            }
            return(result);
        }
Example #3
0
        /// <summary>
        /// Send given request and get its response
        /// </summary>
        public static ResponseEx SendRequest(HttpWebRequest request)
        {
            ResponseEx result = null;

            // if we got a request
            if (request != null)
            {
                HttpWebResponse response     = null;
                MemoryStream    memStream    = null;
                string          errorMessage = null;

                try
                {
                    // now get a response
                    response = (request.GetResponse() as HttpWebResponse);
                }
                catch (WebException wexc)
                {
                    // get the response
                    response = (wexc.Response as HttpWebResponse);

                    // if we have no response
                    if (response == null)
                    {
                        // set an error message
                        errorMessage = "Request failed: " + wexc.Message;
                    }
                }
                catch (Exception exc)
                {
                    // set an error message
                    errorMessage = "Request failed: " + exc.Message;
                }

                // if we got a response
                if ((response != null)
                    // and no error message
                    && string.IsNullOrEmpty(errorMessage))
                {
                    try
                    {
                        // open a stream
                        using (var responseStream = response.GetResponseStream())
                        {
                            // if any
                            if (responseStream != null)
                            {
                                // create the memory stream
                                memStream = new MemoryStream();
                                // create a buffer
                                byte[] buffer = new byte[1024];
                                int    byteCount;

                                // read
                                do
                                {
                                    byteCount = responseStream.Read(buffer, 0, buffer.Length);
                                    memStream.Write(buffer, 0, byteCount);
                                }
                                // read until end of stream
                                while (byteCount > 0);

                                // reset memory stream position
                                memStream.Position = 0;
                            }
                        }
                    }
                    catch (Exception exc)
                    {
                        // set an error message
                        errorMessage = "Exception while reading JSON content of a request to " + exc.Message;
                    }
                }

                // create the result
                result = new ResponseEx(request.RequestUri.ToString(), response, memStream, errorMessage);
            }
            return(result);
        }