You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.5 KiB

  1. //using log4net;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using Swift.API;
  5. using Swift.API.Common;
  6. using Swift.API.Common.Enum;
  7. using Swift.API.Common.TrustDoc;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.IO.Compression;
  12. using System.Linq;
  13. using System.Net;
  14. using System.Net.Http;
  15. using System.Security.Authentication;
  16. using System.Security.Cryptography.X509Certificates;
  17. using System.Text;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. namespace Swift.web.Library
  21. {
  22. public class TrustdocWebAPI
  23. {
  24. public string TrustDocURL { get; set; }
  25. public string TrustDocDataURL { get; set; }
  26. public string Token { get; set; }
  27. public string TrustCertPath { get; set; }
  28. public string TrustCertKey { get; set; }
  29. public TrustdocWebAPI()
  30. {
  31. TrustDocURL = TPGetStatic.ReadWebConfig("TrustDocURL", "");
  32. TrustDocDataURL = TPGetStatic.ReadWebConfig("TrustDocDataURL", "");
  33. Token = TPGetStatic.ReadWebConfig("TrustToken", "");
  34. TrustCertPath = TPGetStatic.ReadWebConfig("TrustCertPath", "");
  35. TrustCertKey = TPGetStatic.ReadWebConfig("TrustCertKey", "");
  36. }
  37. // private static readonly ILog Log = LogManager.GetLogger(typeof(NotifierV2));
  38. public JsonTrustResponse ObtainData(string id, string user, string customerId, string url)
  39. {
  40. DbResult _dbResult = new DbResult();
  41. VerificationStatusResponse _responseModel = new VerificationStatusResponse();
  42. JsonTrustResponse jsonResponse = new JsonTrustResponse();
  43. try
  44. {
  45. ServicePointManager.Expect100Continue = true;
  46. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  47. using (var handler = new WebRequestHandler())
  48. {
  49. handler.ClientCertificates.Add(new X509Certificate2(TrustCertPath, TrustCertKey));
  50. using (var httpClient = new System.Net.Http.HttpClient(handler))
  51. {
  52. httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {Token}");
  53. _dbResult = Utility.LogRequest(user, "TrustDoc", "ObtainData", customerId, url, id);
  54. // NOTE: accept all languages
  55. httpClient.DefaultRequestHeaders.AcceptLanguage.Clear();
  56. httpClient.DefaultRequestHeaders.AcceptLanguage.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("*"));
  57. // NOTE: accept all media types
  58. httpClient.DefaultRequestHeaders.Accept.Clear();
  59. httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("*/*"));
  60. // System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
  61. System.Net.Http.HttpResponseMessage response;
  62. // NOTE: argument zero may be a URL to text, like HTML, or a zip file, image file, PDF file or any other binary data
  63. response = httpClient.GetAsync(url).Result;
  64. if (response.IsSuccessStatusCode)
  65. {
  66. // Read the file.
  67. var contentType = response.Content.Headers.ContentType.ToString().ToLower();
  68. //Stream resultStream = response.Content.ReadAsStreamAsync().Result;
  69. //// The ContentDisposition header contains the name of the file name
  70. //// that you are donwloading.
  71. //string fileName = response.Content.Headers.ContentDisposition.FileName;
  72. //using (var fileStream = File.Create(@"D:\\doc\\" + DateTime.Now.ToString("yyyymmddhhss") + contentType.GetMIMEExtension()))
  73. //{
  74. // resultStream.CopyTo(fileStream);
  75. //}
  76. jsonResponse.code = "0";
  77. jsonResponse.detail = "Success";
  78. jsonResponse.response = response;
  79. jsonResponse.target = contentType;
  80. }
  81. else
  82. {
  83. string resultData = response.Content.ReadAsStringAsync().Result;
  84. ErrorJosn errorJson = JsonConvert.DeserializeObject<ErrorJosn>(resultData);
  85. //JsonResponse jsonResponseData = JsonConvert.DeserializeObject<JsonResponse>(errorJson.Message);
  86. jsonResponse.code = "1";
  87. jsonResponse.detail = errorJson.Message;
  88. jsonResponse.Data = errorJson;
  89. }
  90. }
  91. }
  92. return jsonResponse;
  93. }
  94. catch (Exception ex)
  95. {
  96. var e = new JsonTrustResponse()
  97. {
  98. code = "1",
  99. detail = "Error has occured. Check logs."
  100. };
  101. Utility.LogResponse(_dbResult.Id, _dbResult.Extra2, "999.1", ex.ToString());
  102. return e;
  103. }
  104. }
  105. }
  106. ///NOTE: this class is used to decompress data that is sent compressed, saving bandwidth
  107. }