/// <summary> /// Creates an exception for the response. /// </summary> /// <returns>A new exception for the response.</returns> public WebServiceException CreateException(string?message = null, Exception?innerException = null) { if (m_responseContentPreview == null) { var statusCodeProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(this, m_responseStatusCode?.ToString()); if (statusCodeProperty?.CanRead == true) { const int maxPreviewContentLength = 1000; m_responseContentPreview = JsonUtility.ToJson(statusCodeProperty.GetValue(this)); if (m_responseContentPreview.Length > maxPreviewContentLength) { m_responseContentPreview = m_responseContentPreview.Substring(0, maxPreviewContentLength) + "..."; } } } return(new WebServiceException( message: message, requestMethod: m_requestMethod, requestUri: m_requestUri, responseStatusCode: m_responseStatusCode, responseHeaders: m_responseHeaders, responseContentType: m_responseContentType, responseContentLength: m_responseContentLength, responseContentPreview: m_responseContentPreview, innerException: innerException)); }
/// <summary> /// Overrides HandleResponseCore. /// </summary> protected override async Task <bool> HandleResponseCoreAsync(WebServiceResponseHandlerInfo <TResponse> info) { var response = CreateResponse() !; var webResponse = info.WebResponse !; // find specific status code property bool isStatusCodeHandled = false; HttpStatusCode statusCode = webResponse.StatusCode; string statusCodeText = statusCode.ToString(); var resultProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(response, statusCodeText); object? content = null; if (resultProperty?.CanWrite ?? false) { Type resultPropertyType = resultProperty.PropertyType; content = await ReadContentAsAsync(info, resultPropertyType).ConfigureAwait(false); resultProperty.SetValue(response, content, null); isStatusCodeHandled = true; } var statusCodeProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(response, "StatusCode"); if (statusCodeProperty?.CanWrite ?? false) { Type statusCodePropertyType = statusCodeProperty.PropertyType; if (statusCodePropertyType == typeof(HttpStatusCode)) { statusCodeProperty.SetValue(response, webResponse.StatusCode, null); } else if (statusCodePropertyType == typeof(int)) { statusCodeProperty.SetValue(response, (int)webResponse.StatusCode, null); } else { throw await CreateExceptionAsync(info, "Web response status code cannot be read as {0}.".FormatInvariant(statusCodePropertyType)).ConfigureAwait(false); } isStatusCodeHandled = true; } // make sure status code is handled if (!isStatusCodeHandled) { throw await CreateExceptionAsync(info, "Status code not handled.").ConfigureAwait(false); } // read headers foreach (var header in webResponse.Headers) { // remove hyphens before looking for property setter by name string headerName = header.Key; #pragma warning disable CA1307 // Specify StringComparison for clarity string propertyName = headerName.Replace("-", ""); #pragma warning restore CA1307 // Specify StringComparison for clarity var headerProperty = AutoWebServiceResponseUtility.GetStatusCodeProperty(response, propertyName); if (headerProperty?.CanWrite ?? false) { // get header text string headerText = header.Value.Join("; "); // convert header text to supported types Type headerPropertyType = headerProperty.PropertyType; if (headerPropertyType == typeof(string)) { headerProperty.SetValue(response, headerText, null); } else if (headerPropertyType == typeof(int) || headerPropertyType == typeof(int?)) { headerProperty.SetValue(response, InvariantConvert.ParseInt32(headerText), null); } else if (headerPropertyType == typeof(long) || headerPropertyType == typeof(long?)) { headerProperty.SetValue(response, InvariantConvert.ParseInt64(headerText), null); } else if (headerPropertyType == typeof(Uri)) { headerProperty.SetValue(response, new Uri(webResponse.RequestMessage.RequestUri, headerText), null); } else if (headerPropertyType == typeof(DateTime) || headerPropertyType == typeof(DateTime?)) { headerProperty.SetValue(response, DateTime.ParseExact(headerText, "R", CultureInfo.InvariantCulture), null); } else if (headerPropertyType == typeof(byte[])) { headerProperty.SetValue(response, Convert.FromBase64String(headerText), null); } else { throw await CreateExceptionAsync(info, "Web response header cannot be read as {0}. {1}: {2}".FormatInvariant(headerPropertyType, headerName, headerText)).ConfigureAwait(false); } } } // allow response to read extra data if (response is AutoWebServiceResponse autoWebServiceResponse) { await autoWebServiceResponse.OnResponseHandledAsync(info).ConfigureAwait(false); } // detach response if necessary if (content is WebResponseStream) { info.DetachWebResponse(); } // success info.Response = response; return(true); }