コード例 #1
0
        /// <summary>
        /// Called to create the response.
        /// </summary>
        /// <param name="proposedResponse">The proposed response.</param>
        /// <returns>The response.</returns>
        protected override async Task <WebServiceResponse> CreateResponseAsync(WebServiceResponse proposedResponse)
        {
            HttpStatusCode statusCode = proposedResponse.StatusCode;
            HttpHeaders    headers    = proposedResponse.Headers;
            var            content    = proposedResponse.Content;

            // check for content
            if (content is object)
            {
                // check content type
                if (proposedResponse.HasJson())
                {
                    string responseJson = await proposedResponse.GetJsonAsync().ConfigureAwait(false);

                    // success
                    return(new JsonWebServiceResponse(this, statusCode, headers, JsonWebServiceContent.FromJson(responseJson)));
                }
                else
                {
                    // got content with the wrong content type (HTML error information, perhaps; allowed for non-OK)
                    string message = "Response content type is not JSON: {0}".FormatInvariant(content.Headers.ContentType);
                    if (statusCode == HttpStatusCode.OK)
                    {
                        throw WebServiceResponseUtility.CreateWebServiceException(proposedResponse, message);
                    }
                }
            }

            // missing or non-JSON content
            return(new JsonWebServiceResponse(this, statusCode, headers, content));
        }
コード例 #2
0
        /// <summary>
        /// Gets the JSON.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <returns>The unverified JSON.</returns>
        /// <exception cref="WebServiceException">The response content does not use the JSON content type or the content is empty.</exception>
        public static Task <string> GetJsonAsync(this WebServiceResponse response)
        {
            if (!response.HasJson())
            {
                throw WebServiceResponseUtility.CreateWebServiceException(response, "The response does not have JSON content.");
            }

            return(response.Content !.ReadAsStringAsync());
        }
コード例 #3
0
 /// <summary>
 /// Parses the JSON into an object of the specified type.
 /// </summary>
 /// <typeparam name="T">The specified type.</typeparam>
 /// <param name="response">The response.</param>
 /// <param name="jsonSettings">The JSON settings.</param>
 /// <returns>An object of the specified type.</returns>
 /// <exception cref="WebServiceException">The response content does not use the JSON content type, or the content is empty,
 /// or the text is not valid JSON, or the JSON cannot be deserialized into the specified type.</exception>
 public static async Task <T> GetJsonAsAsync <T>(this WebServiceResponse response, JsonSettings?jsonSettings)
 {
     try
     {
         // parse JSON to desired value
         using var responseStream = await response.Content !.ReadAsStreamAsync().ConfigureAwait(false);
         using var reader         = new StreamReader(responseStream);
         return(JsonUtility.FromJsonTextReader <T>(reader, jsonSettings) !);
     }
     catch (JsonReaderException x)
     {
         // JSON invalid
         throw WebServiceResponseUtility.CreateWebServiceException(response, "Response is not valid JSON: " + x.Message, x);
     }
     catch (JsonSerializationException x)
     {
         // JSON can't be deserialized
         throw WebServiceResponseUtility.CreateWebServiceException(response, "Response JSON could not be deserialized to {0}: {1}".FormatInvariant(typeof(T), x.Message), x);
     }
 }