コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
        }