/// <summary> /// Download the file. /// </summary> /// <param name="Parameters">Query string parameters.</param> /// <returns>The resource as a file.</returns> public async System.Threading.Tasks.Task <SoftmakeAll.SDK.Communication.REST.File> DownloadFileAsync(System.String Parameters) { SoftmakeAll.SDK.Communication.REST REST = new SoftmakeAll.SDK.Communication.REST(); REST.Method = "GET"; REST.URL = $"{SoftmakeAll.SDK.Fluent.SDKContext.APIBaseAddress}/{base.Route}/{Parameters}"; SoftmakeAll.SDK.Communication.REST.File File = await REST.DownloadFileAsync(); SoftmakeAll.SDK.Fluent.SDKContext.LastOperationResult.ExitCode = 0; SoftmakeAll.SDK.Fluent.SDKContext.LastOperationResult.Count = 1; SoftmakeAll.SDK.Fluent.SDKContext.LastOperationResult.Message = ""; SoftmakeAll.SDK.Fluent.SDKContext.LastOperationResult.ID = null; if (REST.HasRequestErrors) { SoftmakeAll.SDK.Fluent.SDKContext.LastOperationResult.ExitCode = (int)REST.StatusCode; SoftmakeAll.SDK.Fluent.SDKContext.LastOperationResult.Count = 0; } return(File); }
public async System.Threading.Tasks.Task <SoftmakeAll.SDK.Communication.REST.File> DownloadFileAsync() { if (System.String.IsNullOrWhiteSpace(this.URL)) { throw new System.Exception(SoftmakeAll.SDK.Communication.REST.EmptyURLErrorMessage); } this.HasRequestErrors = false; this.Method = SoftmakeAll.SDK.Communication.REST.DefaultMethod; SoftmakeAll.SDK.Communication.REST.File Result = null; try { System.Net.HttpWebRequest HttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(this.URL); HttpWebRequest.Method = this.Method; if (this.Headers.Count > 0) { foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> Header in this.Headers) { HttpWebRequest.Headers.Add(Header.Key, Header.Value); } } this.AddAuthorizationHeader(HttpWebRequest); if (this.Body.ValueKind == System.Text.Json.JsonValueKind.Undefined) { HttpWebRequest.ContentLength = 0; } else { System.Byte[] BodyBytes = new System.Text.UTF8Encoding().GetBytes(Body.ToString()); HttpWebRequest.ContentType = SoftmakeAll.SDK.Communication.REST.DefaultContentType; HttpWebRequest.ContentLength = BodyBytes.Length; System.IO.Stream RequestStream = await HttpWebRequest.GetRequestStreamAsync(); await RequestStream.WriteAsync(BodyBytes, 0, BodyBytes.Length); } if (this.Timeout > 0) { HttpWebRequest.Timeout = this.Timeout; } using (System.Net.HttpWebResponse HttpWebResponse = (System.Net.HttpWebResponse) await HttpWebRequest.GetResponseAsync()) { this._StatusCode = HttpWebResponse.StatusCode; System.Net.WebClient WebClient = new System.Net.WebClient(); foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> Header in this.Headers) { WebClient.Headers.Add(Header.Key, Header.Value); } this.AddAuthorizationHeader(WebClient); System.Byte[] FileContents = WebClient.DownloadData(this.URL); if ((FileContents == null) || (FileContents.Length == 0)) { return(null); } System.String FileName = ""; try { const System.String Key = "filename="; FileName = HttpWebResponse.Headers.Get("Content-Disposition"); FileName = FileName.Substring(FileName.IndexOf(Key) + Key.Length); FileName = FileName.Substring(0, FileName.IndexOf(';')); } catch { FileName = ""; } return(new SoftmakeAll.SDK.Communication.REST.File() { Name = FileName, Contents = FileContents }); } } catch (System.Net.WebException ex) { this.HasRequestErrors = true; System.Net.HttpWebResponse HttpWebResponse = ex.Response as System.Net.HttpWebResponse; if (HttpWebResponse == null) { this._StatusCode = System.Net.HttpStatusCode.InternalServerError; Result = null; return(Result); } this._StatusCode = HttpWebResponse.StatusCode; Result = null; } catch { this.HasRequestErrors = true; this._StatusCode = System.Net.HttpStatusCode.InternalServerError; Result = null; } return(Result); }