/// ------------------------------------------------------------------------------------------------ /// /// ------------------------------------------------------------------------------------------------ /// Name GetContentType /// /// <summary> Gets the content type header value based on the ContentType enum. /// </summary> /// <param name="contentType"> The ContentType enum.</param> /// /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ /// public string GetContentType(ContentType contentType) { try { switch (contentType) { case ContentType.BinaryData: return("application/octet-stream"); case ContentType.Json: return("application/json"); case ContentType.Xml: return("application/xml"); default: return("text/plain"); } } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); return(null); } }
/// ------------------------------------------------------------------------------------------------ /// Name GetDocumentType /// /// <summary> Gets the document type mime for the particular file extension. /// </summary> /// <param name="fileExtension"> The file extension.</param> /// /// <returns> string. /// </returns> /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ /// public static string GetDocumentType(this string fileExtension) { try { string source; // switch (fileExtension) { case ".txt": case ".csv": source = "txt.png"; break; case ".doc": case ".docx": source = "doc.png"; break; case ".gif": case ".jpeg": case ".jpg": case ".png": case ".tiff": case ".tif": case ".bmp": source = "png.png"; break; case ".pdf": source = "pdf.png"; break; case ".mp3": case ".wav": source = "mp3.png"; break; case ".mp4": source = "mp4.png"; break; case ".xls": case ".xlsx": source = "xls.png"; break; default: source = "others.png"; break; } return(source); } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); return(null); } }
/// /// ------------------------------------------------------------------------------------------------ /// Name RequestResponseEventArgs /// /// <summary> Creates a new instance of the RequestResponseEventArgs class. /// </summary> /// <param name="data"> The valid response data.</param> /// /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ /// public RequestResponseEventArgs(byte[] data) { try { Data = data; } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); } }
/// ------------------------------------------------------------------------------------------------ #region Public Constructors /// ------------------------------------------------------------------------------------------------ /// /// ------------------------------------------------------------------------------------------------ /// Name RequestResponseEventArgs /// /// <summary> Creates a new instance of the RequestResponseEventArgs class. /// </summary> /// <param name="responseString"> The valid response string.</param> /// /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ /// public RequestResponseEventArgs(string responseString) { try { Text = responseString; } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); } }
/// /// ------------------------------------------------------------------------------------------------ /// Name AddAuthHeader /// /// <summary> Adds an authorisation header /// </summary> /// <param name="token"> The authorisation token.</param> /// /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ /// public void AddAuthHeader(string token) { try { AddHeader("Authorization", string.Format("Bearer {0}", token)); } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); } }
/// ------------------------------------------------------------------------------------------------ #region Public Constructor /// ------------------------------------------------------------------------------------------------ /// /// ------------------------------------------------------------------------------------------------ /// Name ServieClient /// /// <summary> Creates a new instance of the ServiceClient class. /// </summary> /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ /// public ServiceClient() { try { m_oHeaders = new Dictionary <string, string>(); } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); } }
/// ------------------------------------------------------------------------------------------------ #region Static Constructor /// ------------------------------------------------------------------------------------------------ /// /// <summary> It is used to check the network status and update it. /// </summary> /// ------------------------------------------------------------------------------------------------ /// static Reachability() { try { HostName = "www.google.com"; } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); } }
public static float?RevertMt(string miles, string decMiles) { try { float?value = Convert.ToInt32(miles) + Convert.ToSingle(decMiles); return(value == 0 ? null : value); } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); return(null); } }
/// /// ------------------------------------------------------------------------------------------------ /// Name RequestResponseEventArgs /// /// <summary> Creates a new instance of the RequestResponseEventArgs class. /// </summary> /// <param name="ex"> The error exception.</param> /// <param name="url"> The Url that caused the error.</param> /// /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ /// public RequestResponseEventArgs(Exception ex, string url) { try { AuthenticationError = false; Error = ex; Url = url; Body = ""; RequestType = "GET"; } catch (Exception e) { LogTracking.LogTrace(e.ToString()); } }
/// /// ------------------------------------------------------------------------------------------------ /// Name IsHostReachable /// /// <summary> Is the host reachable with the current network configuration. /// </summary> /// <param name="host"> The host.</param> /// /// <returns> If it's reachable. /// </returns> /// ------------------------------------------------------------------------------------------------ /// public static async Task <bool> IsHostReachable(string host) { try { if (string.IsNullOrEmpty(host)) { return(false); } return(await CrossConnectivity.Current.IsReachable(host)); } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); return(false); } }
/// ------------------------------------------------------------------------------------------------ #region Public Functions, Properties and Methods /// ------------------------------------------------------------------------------------------------ /// /// ------------------------------------------------------------------------------------------------ /// Name AddHeader /// /// <summary> Adds a header value to the ServiceClient class to be used on future requests. /// </summary> /// <param name="name"> The name of the header.</param> /// <param name="value"> The value for the header.</param> /// /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ /// public void AddHeader(string name, string value) { try { if (m_oHeaders.ContainsKey(name)) { m_oHeaders[name] = value; } else { m_oHeaders.Add(name, value); } } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); } }
public static Tuple <string, string> ToConvertHt(this float?value) { try { Tuple <string, string> htValue; var splitValue = String.Format("{0:.00}", value).Split('.'); var htHours = (!string.IsNullOrWhiteSpace(splitValue[0]) ? splitValue[0] + "h" : null); var htMin = splitValue.Length > 1 ? GetMinutesFromValue(splitValue) + "m" : null; htValue = new Tuple <string, string>(htHours, htMin); return(htValue); } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); return(null); } }
public static Tuple <string, string> ToConvertMt(this float?value) { try { Tuple <string, string> mtValue; var splitValue = String.Format("{0:.00}", value).Split('.'); var mtMiles = (!string.IsNullOrWhiteSpace(splitValue[0]) ? splitValue[0] : null); var mtDec = splitValue.Length > 1 ? "." + splitValue[1] : null; mtValue = new Tuple <string, string>(mtMiles, mtDec); return(mtValue); } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); return(null); } }
/// /// ------------------------------------------------------------------------------------------------ /// Name RequestResponseEventArgs /// /// <summary> Creates a new instance of the RequestResponseEventArgs class. /// </summary> /// <param name="ex"> The web error exception.</param> /// <param name="url"> The Url that caused the error.</param> /// /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ /// public RequestResponseEventArgs(HttpRequestException ex, string url) { try { AuthenticationError = false; Error = ex; if (ex.Message != null) { if (ServiceClient.StatCode == HttpStatusCode.Unauthorized) { AuthenticationError = true; } Text = ex.Message; } Url = url; Body = ""; RequestType = "GET"; } catch (Exception e) { LogTracking.LogTrace(e.ToString()); } }
/// ------------------------------------------------------------------------------------------------ /// Name OnNetworkChanged /// /// <summary> Executes when the network type is changed. /// </summary> /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ /// public static void OnNetworkChanged(object sender, Plugin.Connectivity.Abstractions.ConnectivityChangedEventArgs e) { try { LoginPage login = new LoginPage(); SplitView.Instace().UpdateStatus(); login.MakeReady(); if ((Application.Current.MainPage.ToString() == "ServiceRequest.Pages.SplitView") && (InternetConnectionStatus() == ReachabilityNetworkStatus.NotReachable)) { DocumentListView.StopProcess?.Invoke(); //PropertySummary.ShowMatchAddress.Invoke(false); } else { //PropertySummary.ShowMatchAddress.Invoke(true); } FullMapView.NewCaseAddView?.LoadButton(); } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); } }
/// /// ------------------------------------------------------------------------------------------------ /// Name UploadStringAsync /// /// <summary> Asynchronously uploads string content to the url. /// </summary> /// <param name="url"> The url for the request.</param> /// <param name="isPut"> Where the method is PUT or POST.</param> /// <param name="content"> The string content of the request.</param> /// <param name="contentType"></param> /// <param name="firstTry"> Indicates that this is the first try at the request.</param> /// /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ public async Task <RequestResponseEventArgs> UploadStringAsync(string url, bool isPut, string content, ContentType contentType, bool firstTry = true) { HttpClient client; RequestResponseEventArgs args; string response; //HttpResponseMessage result; //string response; // try { client = new HttpClient(new NativeMessageHandler()); StatCode = HttpStatusCode.InternalServerError; foreach (string name in m_oHeaders.Keys) { client.DefaultRequestHeaders.Add(name, m_oHeaders[name]); } // StringContent stringContent = new StringContent(content, Encoding.UTF8, GetContentType(contentType)); HttpResponseMessage result = isPut ? await client.PutAsync(url, stringContent) : await client.PostAsync(url, stringContent); if (result.IsSuccessStatusCode) { response = await result.Content.ReadAsStringAsync(); args = new RequestResponseEventArgs(response); } else { StatCode = result.StatusCode; if (result.StatusCode == HttpStatusCode.Unauthorized) { throw new HttpRequestException(); } else { if (result.Content != null) { response = await result.Content.ReadAsStringAsync(); } else { response = result.ReasonPhrase; } // throw new Exception(response); } } } catch (HttpRequestException ex) { if (firstTry && ex.Message.Contains("(ReadDone2)")) { await Task.Delay(500); return(await UploadStringAsync(url, isPut, content, contentType, false)); } args = new RequestResponseEventArgs(ex, url); LogTracking.LogTrace(ex.ToString()); } catch (Exception ex) { args = new RequestResponseEventArgs(ex, url); LogTracking.LogTrace(ex.ToString()); } // if (args.Error != null) { args.Body = content; args.RequestType = isPut ? "PUT" : "POST"; } // return(args); }
/// /// ------------------------------------------------------------------------------------------------ /// Name UploadDataAsync /// /// <summary> Asynchronously uploads data to the url. /// </summary> /// <param name="url"> The url for the request.</param> /// <param name="isPut"> Whether the method is PUT or POST.</param> /// <param name="data"> The data content for the request.</param> /// <param name="contentType"></param> /// <param name="firstTry"> Indicates that this is the first try at the request.</param> /// /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ public async Task <RequestResponseEventArgs> UploadDataAsync(string url, bool isPut, byte[] data, ContentType contentType, bool firstTry = true) { HttpClient client; RequestResponseEventArgs args; HttpResponseMessage result; byte[] response; string stringException; try { client = new HttpClient(new NativeMessageHandler()); StatCode = HttpStatusCode.InternalServerError; foreach (string name in m_oHeaders.Keys) { client.DefaultRequestHeaders.Add(name, m_oHeaders[name]); } ByteArrayContent byteContent = new ByteArrayContent(data); byteContent.Headers.Add("Content-Type", GetContentType(contentType)); result = isPut ? await client.PutAsync(url, byteContent) : await client.PostAsync(url, byteContent); if (result.IsSuccessStatusCode) { response = await result.Content.ReadAsByteArrayAsync(); args = new RequestResponseEventArgs(response); } else { StatCode = result.StatusCode; if (result.StatusCode == HttpStatusCode.Unauthorized) { throw new HttpRequestException(); } else { if (result.Content != null) { stringException = await result.Content.ReadAsStringAsync(); } else { stringException = result.ReasonPhrase; } throw new Exception(stringException); } } args = new RequestResponseEventArgs(response); } catch (HttpRequestException ex) { if (firstTry && ex.Message.Contains("(ReadDone2)")) { await Task.Delay(500); return(await UploadDataAsync(url, isPut, data, contentType, false)); } args = new RequestResponseEventArgs(ex, url); args.Body = string.Format("Data content of length [{0}].", data.Length); args.RequestType = isPut ? "PUT" : "POST"; LogTracking.LogTrace(ex.ToString()); } catch (Exception ex) { args = new RequestResponseEventArgs(ex, url); LogTracking.LogTrace(ex.ToString()); } return(args); }
/// /// ------------------------------------------------------------------------------------------------ /// Name DownloadDataAsync /// /// <summary> Asynchronously downloads binary data from a url. /// </summary> /// <param name="url"> The Url for the request.</param> /// <param name="firstTry"> Indicates that this is the first try at the request.</param> /// /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ /// public async Task <RequestResponseEventArgs> DownloadDataAsync(string url, bool firstTry = true) { HttpClient client; RequestResponseEventArgs args; byte[] response; string stringException; // try { client = new HttpClient(new NativeMessageHandler()); StatCode = HttpStatusCode.InternalServerError; foreach (string name in m_oHeaders.Keys) { client.DefaultRequestHeaders.Add(name, m_oHeaders[name]); } var result = await client.GetAsync(url); if (result.IsSuccessStatusCode) { response = await result.Content.ReadAsByteArrayAsync(); args = new RequestResponseEventArgs(response); } else { StatCode = result.StatusCode; if (result.StatusCode == HttpStatusCode.Unauthorized) { throw new HttpRequestException(); } else { if (result.Content != null) { stringException = await result.Content.ReadAsStringAsync(); } else { stringException = result.ReasonPhrase; } throw new Exception(stringException); } } } catch (HttpRequestException ex) { if (firstTry && ex.Message.Contains("(ReadDone2)")) { await Task.Delay(500); return(await DownloadDataAsync(url, false)); } args = new RequestResponseEventArgs(ex, url); LogTracking.LogTrace(ex.ToString()); } catch (Exception ex) { args = new RequestResponseEventArgs(ex, url); LogTracking.LogTrace(ex.ToString()); } // return(args); }
/// ------------------------------------------------------------------------------------------------ /// Name GetContentType /// /// <summary> Gets the content type mime for the particular file extension. /// </summary> /// <param name="fileExtension"> The file extension.</param> /// /// <returns> string. /// </returns> /// <remarks> /// </remarks> /// ------------------------------------------------------------------------------------------------ /// public static string GetContentType(this string fileExtension) { try { string sContentType; // switch (fileExtension) { case "bmp": sContentType = "image/bmp"; break; case "csv": sContentType = "text/plain"; break; case "doc": case "docx": sContentType = "application/msword"; break; case "dxf": sContentType = "application/dxf"; break; case "dwg": sContentType = "application/dwg"; break; case "dwf": sContentType = "application/dwf"; break; case "eps": sContentType = "image/eps"; break; case "gif": sContentType = "image/gif"; break; case "jpeg": case "jpg": sContentType = "image/jpeg"; break; case "pdf": sContentType = "application/pdf"; break; case "png": sContentType = "image/png"; break; case "rtf": sContentType = "application/rtf"; break; case "tif": case "tiff": sContentType = "image/tiff"; break; case "mp3": case "wav": sContentType = "audio/mp3"; break; case "mp4": sContentType = "video/mp4"; break; case "txt": sContentType = "text/plain"; break; case "xls": case "xlsx": sContentType = "application/msexcel"; break; default: sContentType = "text/plain"; break; } return(sContentType); } catch (Exception ex) { LogTracking.LogTrace(ex.ToString()); return(null); } }