public static string Send(HttpParam param, IHttpException iHttpException = null, HttpClient httpClient = null, CancellationToken cancellationToken = default(CancellationToken)) { using (var client = httpClient ?? new HttpClient()) { HttpRequestMessage request = null; HttpResponseMessage response = null; request = GetHttpRequestMessage(param); response = client.SendAsync(request, cancellationToken).Result; if (response.IsSuccessStatusCode) { return(response.Content.ReadAsStringAsync().Result); } else { var message = $"send api {param.Uri.ToString()} error"; var exception = new HttpException(message, param.Uri.ToString(), param.Method, response); if (iHttpException == null) { throw exception; } iHttpException.SetHttpException(exception); return(null); } } }
/// <summary> /// <para>Sends a response with a HTML payload /// briefly describing the error, including contact information and/or a stack trace /// if specified via the <see cref="ExceptionHandler.ContactInformation"/> /// and <see cref="ExceptionHandler.IncludeStackTraces"/> properties, respectively.</para> /// <para>This handler does not use the <see cref="IHttpException.DataObject">DataObject</see> property.</para> /// </summary> /// <param name="context">An <see cref="IHttpContext" /> interface representing the context of the request.</param> /// <param name="httpException">The HTTP exception.</param> /// <returns>A <see cref="Task" /> representing the ongoing operation.</returns> public static Task HtmlResponse(IHttpContext context, IHttpException httpException) => context.SendStandardHtmlAsync( httpException.StatusCode, text => { text.Write( "<p><strong>Exception type:</strong> {0}<p><strong>Message:</strong> {1}", HttpUtility.HtmlEncode(httpException.GetType().FullName ?? "<unknown>"), HttpUtility.HtmlEncode(httpException.Message)); text.Write("<hr><p>If this error is completely unexpected to you, and you think you should not seeing this page, please contact the server administrator"); if (!string.IsNullOrEmpty(ExceptionHandler.ContactInformation)) { text.Write(" ({0})", HttpUtility.HtmlEncode(ExceptionHandler.ContactInformation)); } text.Write(", informing them of the time this error occurred and the action(s) you performed that resulted in this error.</p>"); if (ExceptionHandler.IncludeStackTraces) { text.Write( "</p><p><strong>Stack trace:</strong></p><br><pre>{0}</pre>", HttpUtility.HtmlEncode(httpException.StackTrace)); } });
public static Task ResponseForHttpException(IHttpContext context, IHttpException httpException) { context.Response.StatusCode = httpException.StatusCode; object data = new { message = httpException.Message }; return(ResponseSerializer.Json(context, data)); }
public static string Send( HttpMethod method, string url, HttpContent content = null, HttpClient client = null, IHttpException iHttpException = null, bool isUseApiAddress = true ) { using (client = client ?? GetClient()) { var param = GetParam(method, url, content, isUseApiAddress); return(HttpRequest.Send(param, httpClient: client, iHttpException: iHttpException)); } }
private async Task HandleHttpExceptionJson(IHttpContext context, IHttpException httpException) { await context.SendStringAsync(JsonConvert.SerializeObject(httpException, Formatting.Indented), MimeType.Json, Encoding.UTF8); }
public static Task DataResponseForHttpException(IHttpContext context, IHttpException httpException) { context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK; return(ResponseSerializer.Json(context, httpException.Message)); }
/// <summary> /// <para>Sends a HTTP exception's <see cref="IHttpException.Message">Message</see> property /// as a plain text response.</para> /// <para>This handler does not use the <see cref="IHttpException.DataObject">DataObject</see> property.</para> /// </summary> /// <param name="context">An <see cref="IHttpContext" /> interface representing the context of the request.</param> /// <param name="httpException">The HTTP exception.</param> /// <returns>A <see cref="Task" /> representing the ongoing operation.</returns> public static Task PlainTextResponse(IHttpContext context, IHttpException httpException) => context.SendStringAsync(httpException.Message ?? string.Empty, MimeType.PlainText, WebServer.DefaultEncoding);
/// <summary> /// Sends an empty response. /// </summary> /// <param name="context">An <see cref="IHttpContext" /> interface representing the context of the request.</param> /// <param name="httpException">The HTTP exception.</param> /// <returns>A <see cref="Task" /> representing the ongoing operation.</returns> #pragma warning disable CA1801 // Unused parameter public static Task EmptyResponse(IHttpContext context, IHttpException httpException) #pragma warning restore CA1801 => Task.CompletedTask;
/// <summary> /// <para>Sends a HTTP exception's <see cref="IHttpException.Message">Message</see> property /// as a plain text response.</para> /// <para>This handler does not use the <see cref="IHttpException.DataObject">DataObject</see> property.</para> /// </summary> /// <param name="context">A <see cref="IHttpContext" /> interface representing the context of the request.</param> /// <param name="httpException">The HTTP exception.</param> /// <returns>A <see cref="Task" /> representing the ongoing operation.</returns> public static Task PlainTextResponse(IHttpContext context, IHttpException httpException) => context.SendStringAsync(httpException.Message, MimeType.PlainText, Encoding.UTF8);
/// <summary> /// Sends an empty response. /// </summary> /// <param name="context">A <see cref="IHttpContext" /> interface representing the context of the request.</param> /// <param name="httpException">The HTTP exception.</param> /// <returns>A <see cref="Task" /> representing the ongoing operation.</returns> public static Task EmptyResponse(IHttpContext context, IHttpException httpException) => Task.CompletedTask;
public static T Send <T>(HttpParam param, IHttpException iHttpException = null, HttpClient httpClient = null, CancellationToken cancellationToken = default(CancellationToken)) { var value = Send(param, iHttpException, httpClient, cancellationToken); return(value.DeserializeObject <T>()); }
/// <summary> /// <para>Sends a HTTP exception's <see cref="IHttpException.Message">Message</see> property /// as a plain text response.</para> /// <para>This handler does not use the <see cref="IHttpException.DataObject">DataObject</see> property.</para> /// </summary> /// <param name="context">An <see cref="IHttpContext"/> interface representing the context of the request.</param> /// <param name="httpException">The HTTP exception.</param> /// <returns>A <see cref="Task"/> representing the ongoing operation.</returns> public static Task PlainTextResponse(IHttpContext context, IHttpException httpException) => context.SendStringAsync(Validate.NotNull(nameof(httpException), httpException).Message ?? string.Empty, MimeType.PlainText, Encoding.UTF8);
public static async Task Handle(IHttpContext ctx, IHttpException exception) { $"HTTP Error Caught: {ctx.RemoteEndPoint.Address} ({exception.StatusCode}) {exception.Message} {ctx.RequestedPath}".Warn(LogSource); await ctx.SendDataAsync(new {Message = ((HttpStatusCode)exception.StatusCode).ToString()}); }