The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

HttpWebResponseResult.cs 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Net;
  6. namespace Teknik.Utilities
  7. {
  8. /// <summary>
  9. /// Result for relaying an HttpWebResponse
  10. /// </summary>
  11. public class HttpWebResponseResult : ActionResult
  12. {
  13. private readonly HttpWebResponse _response;
  14. private readonly ActionResult _innerResult;
  15. /// <summary>
  16. /// Relays an HttpWebResponse as verbatim as possible.
  17. /// </summary>
  18. /// <param name="responseToRelay">The HTTP response to relay</param>
  19. public HttpWebResponseResult(HttpWebResponse responseToRelay)
  20. {
  21. if (responseToRelay == null)
  22. {
  23. throw new ArgumentNullException("response");
  24. }
  25. _response = responseToRelay;
  26. Stream contentStream;
  27. if (responseToRelay.ContentEncoding.Contains("gzip"))
  28. {
  29. contentStream = new GZipStream(responseToRelay.GetResponseStream(), CompressionMode.Decompress);
  30. }
  31. else if (responseToRelay.ContentEncoding.Contains("deflate"))
  32. {
  33. contentStream = new DeflateStream(responseToRelay.GetResponseStream(), CompressionMode.Decompress);
  34. }
  35. else
  36. {
  37. contentStream = responseToRelay.GetResponseStream();
  38. }
  39. if (string.IsNullOrEmpty(responseToRelay.CharacterSet))
  40. {
  41. // File result
  42. _innerResult = new FileStreamResult(contentStream, responseToRelay.ContentType);
  43. }
  44. else
  45. {
  46. // Text result
  47. var contentResult = new ContentResult();
  48. contentResult = new ContentResult();
  49. contentResult.Content = new StreamReader(contentStream).ReadToEnd();
  50. _innerResult = contentResult;
  51. }
  52. }
  53. public override void ExecuteResult(ActionContext context)
  54. {
  55. var clientResponse = context.HttpContext.Response;
  56. clientResponse.StatusCode = (int)_response.StatusCode;
  57. foreach (var headerKey in _response.Headers.AllKeys)
  58. {
  59. switch (headerKey)
  60. {
  61. case "Content-Length":
  62. case "Transfer-Encoding":
  63. case "Content-Encoding":
  64. // Handled by IIS
  65. break;
  66. default:
  67. clientResponse.Headers.Add(headerKey, _response.Headers[headerKey]);
  68. break;
  69. }
  70. }
  71. _innerResult.ExecuteResult(context);
  72. }
  73. }
  74. public enum ResultType
  75. {
  76. Passthrough,
  77. Json
  78. }
  79. }