//using log4net; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Swift.API; using Swift.API.Common; using Swift.API.Common.Enum; using Swift.API.Common.TrustDoc; using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Net; using System.Net.Http; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Swift.web.Library { public class TrustdocWebAPI { public string TrustDocURL { get; set; } public string TrustDocDataURL { get; set; } public string Token { get; set; } public string TrustCertPath { get; set; } public string TrustCertKey { get; set; } public TrustdocWebAPI() { TrustDocURL = TPGetStatic.ReadWebConfig("TrustDocURL", ""); TrustDocDataURL = TPGetStatic.ReadWebConfig("TrustDocDataURL", ""); Token = TPGetStatic.ReadWebConfig("TrustToken", ""); TrustCertPath = TPGetStatic.ReadWebConfig("TrustCertPath", ""); TrustCertKey = TPGetStatic.ReadWebConfig("TrustCertKey", ""); } // private static readonly ILog Log = LogManager.GetLogger(typeof(NotifierV2)); public JsonTrustResponse ObtainData(string id, string user, string customerId, string url) { DbResult _dbResult = new DbResult(); VerificationStatusResponse _responseModel = new VerificationStatusResponse(); JsonTrustResponse jsonResponse = new JsonTrustResponse(); try { ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; using (var handler = new WebRequestHandler()) { handler.ClientCertificates.Add(new X509Certificate2(TrustCertPath, TrustCertKey)); using (var httpClient = new System.Net.Http.HttpClient(handler)) { httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {Token}"); _dbResult = Utility.LogRequest(user, "TrustDoc", "ObtainData", customerId, url, id); // NOTE: accept all languages httpClient.DefaultRequestHeaders.AcceptLanguage.Clear(); httpClient.DefaultRequestHeaders.AcceptLanguage.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("*")); // NOTE: accept all media types httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("*/*")); // System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; System.Net.Http.HttpResponseMessage response; // NOTE: argument zero may be a URL to text, like HTML, or a zip file, image file, PDF file or any other binary data response = httpClient.GetAsync(url).Result; if (response.IsSuccessStatusCode) { // Read the file. var contentType = response.Content.Headers.ContentType.ToString().ToLower(); //Stream resultStream = response.Content.ReadAsStreamAsync().Result; //// The ContentDisposition header contains the name of the file name //// that you are donwloading. //string fileName = response.Content.Headers.ContentDisposition.FileName; //using (var fileStream = File.Create(@"D:\\doc\\" + DateTime.Now.ToString("yyyymmddhhss") + contentType.GetMIMEExtension())) //{ // resultStream.CopyTo(fileStream); //} jsonResponse.code = "0"; jsonResponse.detail = "Success"; jsonResponse.response = response; jsonResponse.target = contentType; } else { string resultData = response.Content.ReadAsStringAsync().Result; ErrorJosn errorJson = JsonConvert.DeserializeObject(resultData); //JsonResponse jsonResponseData = JsonConvert.DeserializeObject(errorJson.Message); jsonResponse.code = "1"; jsonResponse.detail = errorJson.Message; jsonResponse.Data = errorJson; } } } return jsonResponse; } catch (Exception ex) { var e = new JsonTrustResponse() { code = "1", detail = "Error has occured. Check logs." }; Utility.LogResponse(_dbResult.Id, _dbResult.Extra2, "999.1", ex.ToString()); return e; } } } ///NOTE: this class is used to decompress data that is sent compressed, saving bandwidth }